Find rows of cell array containing certain specific multiple cells?

8 ビュー (過去 30 日間)
Song Decn
Song Decn 2020 年 2 月 22 日
編集済み: Giuseppe Inghilterra 2020 年 2 月 22 日
Suppose I have a cell array,
ca = {'a' 'b' 'c' 10 10; 'c' 'e' 'f' 10 20; 'g' 'h' 'i' 20 10;}
How can I find the row containing eg. {'a'} {'b'} {'c'} at same time?
  2 件のコメント
David Hill
David Hill 2020 年 2 月 22 日
What are you trying to do? Does your cell array need to stay the way it is? Why not group the data into matrices with the same data.
ca={['abc';'cef';'ghi'],[10 10;10 20;20 10]};
[idx1,idx2]=find(ca{1}=='a');
Song Decn
Song Decn 2020 年 2 月 22 日
Sorry David, to be clear, I want to find out the row in cell array 'ca', containing {'a'} {'b'} {'c'} at same time.

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

採用された回答

Giuseppe Inghilterra
Giuseppe Inghilterra 2020 年 2 月 22 日
編集済み: Giuseppe Inghilterra 2020 年 2 月 22 日
Hi,
try to run following code, variable 'r' returns you row that contains what are you looking for:
[r c] = find(strcmp(ca,'g'));
Hope this helps.
Edit:
ValueToFind = {'g' 'h' 'i'}; %Define cell array of values to find in same row
l = length(ValueToFind);
ca = {'a' 'b' 'c' 10 10; 'c' 'e' 'f' 10 20; 'g' 'h' 'i' 20 10;};
[r_ca ~] = size(ca);
Row = zeros(r_ca,l);
for ii = 1:l
[r ~] = find(strcmp(ca,ValueToFind(ii)));
if ~isempty(r)
r = sort(r);
Row(r,ii) = 1;
end
end
r_f = find(all(Row,2));
In this way you obtain the row (r_f) where all ValueToFind are. Right now I give you this solution, by adopting a for loop cycle. I dont think that exists a one-line solution.
  1 件のコメント
Song Decn
Song Decn 2020 年 2 月 22 日
Thank u Giuseppe. I want to find out the row in cell array 'ca', containing {'a'} {'b'} {'c'} at same time, not a row containing a single element like 'a' or 'b' or 'g'

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

その他の回答 (0 件)

カテゴリ

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

タグ

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by