Search through Cell

9 ビュー (過去 30 日間)
Charles
Charles 2011 年 8 月 19 日
Hi, I have problem working with cells.
I have a 1 x 4 cell with each cell containing an N X 1 column vector. How do I search through all the first three cell elements for the rows having a given value.
To illustrate further, if I a matrix, A = N X 4; and want to find where the values 0 occur, I do
[row column] = find(A(:,1:3) == 0);
So, essentially what I am looking for is its equivalent command for a cell.

採用された回答

Jan
Jan 2011 年 8 月 19 日
What about a trivial FOR loop:
C = {rand(100, 1), rand(100, 1), rand(100, 1), rand(100, 1)};
value = 0.5;
row = cell(1, 3);
column = cell(1, 3);
for i = 1:3
[row{i}, column{i}] = find(C{1} == value);
end
This can be done by CELLFUN also, but I do not see an advantage.
Or you can create the numerical matrix explicitely and use logical indexing:
Match = find(cat(2, C{1:3}) == value);
Usually using the columns of the logical matrix Match is faster than using separate index vectors.
  1 件のコメント
Charles
Charles 2011 年 8 月 19 日
Hi, thanks for your response. A loop would be too expensive as there I have more than 20 million rows. What I did initially was to create a matrix from the cell and then search using the method that I described before, but I did not want to create extra variables. Anyway, I think your last suggestion will do it.

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

その他の回答 (1 件)

Daniel Shub
Daniel Shub 2011 年 8 月 19 日
A = cell2mat(A);
[row column] = find(A(:,1:3) == 0);
A = mat2cell(x, N, [1, 1, 1, 1]);

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by