0

I have two cell arrays.

A(290*6) and B(300*6);

First column in two arrays are identical. I compared first column of two cell arrays using 'ismember'. I want to do that ; where cell elements are missing in cell array(A), I have to add a row where element is missing. Is it possible in Matlab?

Jino
  • 345
  • 1
  • 2
  • 16
user3248216
  • 17
  • 2
  • 6
  • 3
    This is a little vague, please give mock example data with A as say a 3-by-1 and B as a 5-by-1 and the exact output you desire (for those minimal mock examples) – Dan Mar 13 '14 at 08:35

2 Answers2

0

It's not easy to insert rows into an existing matrix or cell array; it's easier to construct a new one and fill it appropriately.

Find the locations of the contents of the first column of A in the cell array B:

[aa,bb] = ismember([A{:,1}],[B{:,1}]);

Create a new empty cell array:

C = cell(length(B),size(A,2))

Fill it:

C(:,1)=B(:,1)
C(bb,2:end) = A(aa,2:end);

For example, given this A ("3" row missing)

[1]    [3]
[2]    [5]
[4]    [3]

And this B:

[1]
[2]
[3]
[4]

This returns:

[1]    [3]
[2]    [5]
[3]     []
[4]    [3]

To fill the empty spaces with the previous row (this will only work if the empty rows are non-consecutive and the first row of C is non-empty):

n = setdiff(1:length(C),bb)
C(n,2:end) = C(n-1,2:end);
nkjt
  • 7,825
  • 9
  • 22
  • 28
  • Thank you very much. It worked. I am sry that my question is not complete. Can I fill those empty cells with the data that is above that empty cell. Sry for the inconvenience for posting question here. – user3248216 Mar 14 '14 at 05:25
  • Thank you very much. It worked. I am sry that my question is not complete. Can I fill those empty cells with the data that is above that empty cell. Sry for the inconvenience for posting question here. For example that is shown, empty cell should be replace by [5]. Can this be possible. – user3248216 Mar 14 '14 at 05:55
  • See edit - although you might need to play around a little with it depending on the distribution of the empty rows. – nkjt Mar 14 '14 at 11:30
0

I think you can directly use the second output of setdiff

[d,i] = setdiff(B(:,1),A(:,1))

i will tell you where the rows in A are that are missing.

bdecaf
  • 4,652
  • 23
  • 44