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?

1
  • 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) Commented Mar 13, 2014 at 8:35

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

3 Comments

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.
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.
See edit - although you might need to play around a little with it depending on the distribution of the empty rows.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.