I'm trying to compare components of an array using relational operators, and the outcome needs to be either true or false. My result is a row vector of logicals; how do i make sure the result is just a single 1 or 0?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
j8099
el 19 de Sept. de 2014
Comentada: Image Analyst
el 19 de Sept. de 2014
A=rand(15,10); B=any(A(8,:))==max(A); B= 0 0 0 0 0 0 0 0 0 0
0 comentarios
Respuesta aceptada
Stephen23
el 19 de Sept. de 2014
Editada: Stephen23
el 19 de Sept. de 2014
Note that like many MALTAB functions, max , any and all work along one (default or specified) dimension. If you wish for these function to operate on every element in the array, then you can use the syntax (:), regardless of the size of the array, eg:
any(X(:))
So one solution for your code would be:
A = rand(15,10);
B = any(A(:)==max(A(:)));
Unless you need that (8,:) indexing for some other purpose...
0 comentarios
Más respuestas (1)
Guillaume
el 19 de Sept. de 2014
You've misplaced a bracket:
B=any(A(8,:) == max(A));
2 comentarios
Image Analyst
el 19 de Sept. de 2014
japna's "Answer" moved here:
Oh Sorry! I carried it out on the command window correctly, with the bracket in place, and the answer it is giving me is correct....i just don't know how to convert that row vector of logicals to a single 1 or 0
A=rand(15,10);
B=any(A(8,:))==max(A);
B= 0 0 0 0 0 0 0 0 0 0
Image Analyst
el 19 de Sept. de 2014
Guillaume's reply moved here:
Please comment on the answer rather than starting a new answer.
I meant to say, that for it to work, you need to move the bracket as I've shown in my answer. You get a scalar.
any (if you want true for at least one true) or all (if you require every element to be true) is what you use to transform a vector of logical to a scalar.
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!