counting the number of values in table if the condition satisfies??

34 ビュー (過去 30 日間)
Haritha
Haritha 2019 年 3 月 27 日
コメント済み: Guillaume 2019 年 3 月 28 日
Hi,
I have 2 tables as
a =
y=
If the string in "y" matches with string in ''a'' i want to count all the strings after matching the strings.
I am looking for looping only not like count command.
Thanks in advance
  9 件のコメント
Haritha
Haritha 2019 年 3 月 27 日
編集済み: Haritha 2019 年 3 月 27 日
I have attached sample code. If string in U matches with string in xx I want to count the strings after matching string in xx. I have only one column as xx
Guillaume
Guillaume 2019 年 3 月 27 日
編集済み: Guillaume 2019 年 3 月 27 日
Why is U a table as opposed to the simpler
U = 'car';
?
Why is xx a table as opposed to simply a cell array?
You don't seem to know how to index cell arrays, tables or matrices. If A is matrix, A{i} is an error.
So in your example, is the desired result 4? (4 strings after 'car')

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

採用された回答

dpb
dpb 2019 年 3 月 27 日
xx = categorical({'computer'; 'car'; 'mobile'; 'bus'; 'tree'; 'jeep'});
u='car';
[ia,ib]=ismember(u,xx);
if ia
count=numel(xx)-2+1; % inclusive count
else
count=nan; % not found indicator
end
tables unnecessarily complicate life when they really have no useful reason for using them in a given case.
Using categorical for such variables simplilfies comparisons greatly; that's what they're for.
Above for a one-at-a-time lookup, loop over an array of u if needed...
  2 件のコメント
Haritha
Haritha 2019 年 3 月 28 日
Ok Finally i will change it to cell array then i will use this other than table matching cells matching is fine
Guillaume
Guillaume 2019 年 3 月 28 日
That count line should be:
count = numel(XX) - in + 1;
However, you could just replace the whole lot by:
count = numel(XX) - find(u == XX, 1);
In the above case, there's no benefit to using categorical. You could just stick to a cell array, in which case you replace the == by strcmp

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

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by