필터 지우기
필터 지우기

Calling a indices using 'find' function

조회 수: 1 (최근 30일)
Ethan Boyce
Ethan Boyce 2020년 9월 5일
댓글: Walter Roberson 2020년 9월 8일
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?

채택된 답변

Walter Roberson
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
Ethan Boyce
Ethan Boyce 2020년 9월 5일
Understood, thank you. A question about the syntax in your suggestion, what does the input of 1 reference?
Walter Roberson
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 CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by