Hey
I have a 3x20 array, where I want to check if row 2 contains 1. If not it should state a message. I was thinking on a if & else . But I'm not quite sure how to check for the number 1.

 채택된 답변

Cedric
Cedric 2014년 5월 6일
편집: Cedric 2014년 5월 6일

3 개 추천

Here is an example:
A = randi( 10, 3 ,20 ) % Dummy example.
if any( A(2,:) == 1 )
fprintf( 'Found at least one 1 on row 2!\n' ) ;
end
Running it gives:
>> A
A =
Columns 1 through 16
1 7 5 1 6 1 3 10 7 4 4 10 6 9 4 4
3 1 2 3 6 9 1 7 5 9 4 9 2 7 6 7
2 7 4 4 8 4 9 6 10 2 10 2 9 3 8 5
Columns 17 through 20
2 6 1 6
7 8 5 10
6 2 9 7
Found at least one 1 on row 2!

댓글 수: 6

Kasper
Kasper 2014년 5월 6일
편집: Kasper 2014년 5월 6일
When I run it
if any( FCA(2,:) == 1 );
fprintf( 'Found at least one 1 on row 2!\n' ) ;
end
i get the message:
Undefined function 'eq' for input arguments of type 'cell'.
Don't have eq anywhere.
edit
I just realized I wrote it wrong. It is a 20x3 matrix, not 3x20. But still get the message
Cedric
Cedric 2014년 5월 6일
편집: Cedric 2014년 5월 6일
It is because FCA is not a numeric array, but a cell array [ ref ]. Try to convert it to a numeric array first, i.e.:
FCA_num = cell2mat( FCA ) ;
and then work with FCA_num.
Kasper
Kasper 2014년 5월 6일
편집: Kasper 2014년 5월 6일
I guess I should have provided that information, but row 1 is text. So MatLab gives me:
Error using cell2mat (line 46)
All contents of the input cell array must be of the same data type.
Cedric
Cedric 2014년 5월 6일
편집: Cedric 2014년 5월 6일
Then convert just row 2 if it is the only row involved.
row2 = cell2mat( FCA(2,:) ) ;
if any( row2 == 1 )
...
end
Kasper
Kasper 2014년 5월 6일
That worked!
Thank you!
Cedric
Cedric 2014년 5월 6일
My pleasure.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기

질문:

2014년 5월 6일

댓글:

2014년 5월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by