How to find matrix values by column and row indexes

조회 수: 2 (최근 30일)
m h
m h 2021년 2월 5일
편집: m h 2021년 2월 8일
Hi,
I have a large m x n matrix, let's say "A" and I would like to find the location indexes of minimum values(negative or positive) in each column and confirm that location. So, I used sth like this
>> temp0=A
>> temp1=abs(A)
>> sorted_mat=sort(temp1,1,'ascend');
>> [rw0, col0]=find(temp1==sorted_mat(1,:));
>> For i=1:size(rw0,1)
>> min_values(i)=temp0(rw0(i),col0(i))
>> end
However, I would like to use indexes directly. I had this method
>> temp0=A
>> temp1=abs(A)
>> sorted_mat=sort(temp1,1,'ascend');
>> [rw0, col0]=find(temp1==sorted_mat(1,:));
>> min_values=nonzeros(A(rw0,col0).*eye(size(rw0,1))
The problem with this method is that it doesn't work with really large numbers like 60k. Do you have any idea to solve it by indexes? I don't like using for loops. :)

답변 (1개)

Sourabh Kondapaka
Sourabh Kondapaka 2021년 2월 8일
편집: Sourabh Kondapaka 2021년 2월 8일
Hi @m h,
You can find min-values in a matrix either column-wise or row-wise without using loops. Please check the documentation for the "min()" function.
You can find similar questions answered in this community:
  댓글 수: 1
m h
m h 2021년 2월 8일
편집: m h 2021년 2월 8일
Hi,
Actually, I am not looking for min or max values in my matrix. What I am looking for is, the closest point to certain point that changes in time in 1D environment. If the closest point is behind the target point, it will have negative values or vice versa. So, the minimum values above indicates closest point, not the minimum values in my matrix. I can write code with minimum function like
>> temp0=A
>> temp1=abs(A)
>>[Mx idx]=min(temp1,[],1]
>>[Mx idy]=min(temp1,[],2]
>> For i=1:size(idx,1)
>> searched_values(i)=temp0(idx(i),idy(i))
>> end
My question is whether there is a way to express something like below
->> searched_values=temp0(idx,idy) (this doesn't work)
( searched_values=nonzeros(A(rw0,col0).*eye(size(rw0,1)) % I had this but it doesn't work due to matrix size.)
instead of for loop.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by