Check for 1 in a array

38 Ansichten (letzte 30 Tage)
Kasper
Kasper am 6 Mai 2014
Kommentiert: Cedric am 6 Mai 2014
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.

Akzeptierte Antwort

Cedric
Cedric am 6 Mai 2014
Bearbeitet: Cedric am 6 Mai 2014
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 Kommentare
Kasper
Kasper am 6 Mai 2014
That worked!
Thank you!
Cedric
Cedric am 6 Mai 2014
My pleasure.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by