Calling a indices using 'find' function
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Ethan Boyce
el 5 de Sept. de 2020
Comentada: Walter Roberson
el 8 de Sept. de 2020
I guess the easiest way to ask this question is to show first,
a = [1 -1 2 -1; 2 -2 3 -3;1 1 1 0; 1 -1 4 3];
[row,col] = find(a==maxk(abs(a(:,3)),1))
Is there then a way to assign say a( __,:) to the row that is returned in this function? For example, If I want to swap a(1,:) with the row a(__,:) found from this find command, how would I go about it?
0 comentarios
Respuesta aceptada
Walter Roberson
el 5 de Sept. de 2020
a==maxk(abs(a(:,3)),1)
If you know for sure that a(:,3) is always positive, then the abs() is not needed.
If you do not know for sure that a(:,3) is always positive, then it is possible that the element with largest absolute value is a negative element, in which case there might not be any locations in a that equal the absolute value of that negative element -- row and col might be empty, in which case it makes not sense to swap.
If you know for sure that there will be exactly one entry in a equal to the entry in a(:,3) with maximal absolute value, then you might as well use a simpler flow with just a max() call, getting out a row index, and knowing that the column index must be 3 for what you locate.
If you do not know for sure that there will be exactly one entry in a equal to the entry in a(:,3) with maximal absolute value then row and col might be be empty or might be vectors; in either case it is not clear what swapping would mean.
It is possible (at least to outside observers) that the maximal absolute value is in row 1, in which case it is not clear what it means to do the swapping.
Anyhow, the answer you are looking for is:
a([1 row], :) = a([row 1], :); %swap rows.
... keeping in mind the reasons I described why your code could fail.
2 comentarios
Walter Roberson
el 8 de Sept. de 2020
The 1 refers to "a(1,:)" that you want to swap with, if you are referring to the swapping code I posted.
If you are referring to the maxk, then then 1 is the number of maximum values to find, and is copied from your code.
Más respuestas (0)
Ver también
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!