MATLAB Answers

How to find matrix values by column and row indexes

2 views (last 30 days)
m h
m h on 5 Feb 2021
Edited: m h on 8 Feb 2021
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. :)

Answers (1)

Sourabh Kondapaka
Sourabh Kondapaka on 8 Feb 2021
Edited: Sourabh Kondapaka on 8 Feb 2021
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 Comment
m h
m h on 8 Feb 2021
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.

Sign in to comment.

Tags

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by