How to find matrix values by column and row indexes

3 次查看(过去 30 天)
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
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