How to call out rows that contains a value X in column Z of matrix A?

4 ビュー (過去 30 日間)
sono
sono 2012 年 10 月 12 日
If I have a matrix A what command would return the number of each row in A that contains the value X in column Z?
EXAMPLE:
A:
20.0000 1.0000 8.3333
2.0000 2.0000 32.6667
45.0000 1.0000 5.3333
4.0000 1.0000 8.3333
54.0000 3.0000 25.0000
38.0000 3.0000 33.3333
7.0000 2.0000 16.6667
8.0000 1.0000 10.3333
somefunction(3,A(:,2)) = [5,6]
5 and 6 being the rows in A that contain the value 3 in column 2
Thank you!

採用された回答

the cyclist
the cyclist 2012 年 10 月 12 日
find(A(:,Z)==X)
in general, and
find(A(:,2)==3)
for the specific case you mention.
  2 件のコメント
sono
sono 2012 年 10 月 12 日
Unfortunately that doesn't seem to work:
>> find(A(:,2)==3)
ans =
[]
the cyclist
the cyclist 2012 年 10 月 13 日
編集済み: the cyclist 2012 年 10 月 13 日
Are you sure that your values in the second column are _exactly _equal to 2? Or might they be slightly off, due to floating point precision?
I think you fill find the following code does what you expect:
A = [20.0000 1.0000 8.3333
2.0000 2.0000 32.6667
45.0000 1.0000 5.3333
4.0000 1.0000 8.3333
54.0000 3.0000 25.0000
38.0000 3.0000 33.3333
7.0000 2.0000 16.6667
8.0000 1.0000 10.3333]
find(A(:,2)==3)

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

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2012 年 10 月 12 日
If you have a cell array
A={'Value' 'Count' 'Percent'
1 1 '10.00%'
2 3 '30.00%'
3 1 '10.00%'
4 1 '10.00%'
5 0 '0.00%'
6 0 '0.00%'
7 3 '30.00%'
8 0 '0.00%'
9 1 '10.00%'}
out=find(cell2mat((A(2:end,2)))==3)
  1 件のコメント
sono
sono 2012 年 10 月 12 日
Thanks.
I think working with a non cell array is simpler for me right now but I'll keep it in mind for future use.

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by