Find cell and return index

1 回表示 (過去 30 日間)
Ting-Yu Chueh
Ting-Yu Chueh 2019 年 4 月 20 日
回答済み: Image Analyst 2019 年 4 月 20 日
Hi everyone,
a = { {21 3 1}, {22 32 2}, {21 31 2}, {22 32 1} };
I would like to know the locations of the cells containing a specific value- {21 31 1}
In this case, it's 1.
I try to use like below:
Match = {21 31 1};
CorrectMatch = find (cellfun(@(x) isequal (x,Match), a(1,:)));
but it doesn't work out
Could any one help me?
  1 件のコメント
Walter Roberson
Walter Roberson 2019 年 4 月 20 日
Use a numeric vector for Match because cellfun will be passing in the content of the cells

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

採用された回答

Image Analyst
Image Analyst 2019 年 4 月 20 日
Your Match is not a pattern in your "a" so that's why it didn't find it. {21 31 1} is nowhere in a. If it still does not work and you want a simple, intuitive for loop, then try this:
a = { {21 3 1}, {22 32 2}, {21 31 2}, {22 32 1} };
% Define what we want to find
pattern = {21 3 1};
% Instantiate output vector of where matches were found.
matches = false(size(a));
for col = 1 : length(a)
if isequal(a{col}, pattern)
matches(col) = true;
end
end
matches % Show matches in command window.

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by