how to compare two matrix with if else statement?

3 visualizaciones (últimos 30 días)
Ayesha Punjabi
Ayesha Punjabi el 26 de Ag. de 2019
Comentada: Walter Roberson el 26 de Ag. de 2019
select_win = [4 5 8 9]
mat_t = [0 0 0 0; 1 0 1 0; 0 1 0 0]
data = [1 0 1 0];
I am trying to compare data matrix with mat_t.
if data array matches with mat_t array I want to write a condition of selecting first row from select_win array else I want to choose 2nd row from select_win array
I am trying to write an if else statement in this but its not working. I am stuck at the point in my code and would genuinely appreciate if someone helps with it.

Respuestas (2)

Walter Roberson
Walter Roberson el 26 de Ag. de 2019
? Your select_win only has one row.
Question: is it the corresponding element that is to be chosen?
For row K of mat_t :
select_win( sub2ind(size(select_win), (data == mat_t(K,:)) + 1, 1:size(data,2)) )
  3 comentarios
Ayesha Punjabi
Ayesha Punjabi el 26 de Ag. de 2019
But its not working properly because if changes mat_t 2nd row to all zeros and it displays 5 which is 2nd column from select_win array
Walter Roberson
Walter Roberson el 26 de Ag. de 2019
if data==mat_t
select_win(1)
else data~=mat_t
select_win(2)
end
means the same as
if all( reshape(data==mat_t, [], 1) )
disp(select_win(1))
else
disp(data~=mat_t)
disp(select_win(2))
end
There are elemenets of the vector data that are not equal to the corresponding entries in mat_t, so it is not true that all of the elements are equal, so the else is invoked.

Iniciar sesión para comentar.


Matt J
Matt J el 26 de Ag. de 2019
Editada: Matt J el 26 de Ag. de 2019
rownum = all(data==mat_t,2)+1;
select_win( rownum )

Categorías

Más información sobre Logical 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!

Translated by