MATLAB Answers

Compare the rows of two cell arrays?

18 ビュー (過去 30 日間)
Ibro Tutic
Ibro Tutic 2017 年 7 月 21 日
回答済み: Ari 2017 年 7 月 21 日
Let's say we have two cell arrays, a and b. a={1, 2; 3, 4; 5, 6; 7, 8} and b={1, 2; 3, 4; 5, 6; 7, 8}. How would I go about comparing the two arrays such that each row is compared to make sure that each row in b only contains numbers from the same row of a. So for example, the a(1,:)={1, 2} and b(1,:)={1,2}. This is fine, however, if b(1,:)={4,2}, an error would popup. Also, using the example from above, if b(1,:)={1}, this would be fine also. Length doesn't matter as long as its not longer than the same row of a. Any ideas?

  2 件のコメント

Image Analyst
Image Analyst 2017 年 7 月 21 日
Why are you using cells arrays for a and b instead of regular numerical arrays?
Also b(1:,:) is a 1 by 2 cell array - the first row of b - so it cannot be {1} since that is only one cell, not two cells like you'd need to have an entire row of b.
Ibro Tutic
Ibro Tutic 2017 年 7 月 21 日
Sorry, the cell arrays are used because they are inputs from (b is the inputs)
inputdlg
a is the cell array that comes from some data that is pulled from various files. the data comes in as strings and is stored as a cell array.
And I thought I might have made that mistake but I wasn't sure. I guess I just need to compare the number to make sure it still matches a number in a.

サインインしてコメントする。

回答 (1 件)

Ari
Ari 2017 年 7 月 21 日
It will be a good idea to convert the cell arrays to numeric matrices before doing the comparison. The following line of code will return a logical array with zeros in rows which do not satisfy your condition.
check = ismember(cell2mat(b), cell2mat(a), 'rows')

  0 件のコメント

サインインしてコメントする。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by