Calling a indices using 'find' function
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Walter Roberson
2020-9-5
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 个评论
Walter Roberson
2020-9-8
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!