Row and column of minim value from 3D matrix
3 次查看(过去 30 天)
显示 更早的评论
Hello,
I've been looking for a method to do this since yesterday. I found some helpful answers here and the one which helps me the most still does not give me the best result.
I am trying to find the row and column of a matrix A = (:,:,eachUser) .
The column values are the one I need but the row values are totally different from what I have to obtain, unless the first one ( out of 10) which is always ok.
Any suggestions would be much appreciated. Thanks!
LE: I have the matrix A(row,column,User) and for each User I have the rows and columns. I want the minimum value from each row x column for each USER and it's index in order to calculate the exact position in the A matrix.
for eachUser = 1:length(Users_coordinates)
UsersDistance(:,:,eachUser) = ManhattanDistanceSum_2(:,:,eachUser);
[minimumV1(:,:,eachUser),indexMinimFirst(:,:,eachUser)]= min(UsersDistance(:,:,eachUser));
[minimumV2(:,:,eachUser) , indexMinimSecond(:,:,eachUser)] = min(minimumV1(:,:,eachUser));
rowV2(:,:,eachUser) = ind2sub(size(UsersDistance(:,:,eachUser)),indexMinimFirst(indexMinimSecond(:,:,eachUser)));
columnV2(:,:,eachUser) = ind2sub(size(UsersDistance(:,:,eachUser)),indexMinimSecond(:,:,eachUser));
% ErrorPositioning(eachUser) = myerrordistance(row(:,:,eachUser),Users_coordinates(eachUser,1), col(:,:,eachUser),Users_coordinates(eachUser,2));
end
0 个评论
采纳的回答
dpb
2021-7-15
编辑:dpb
2021-7-15
I'm going to shorten variable names...
nP=size(X,3); % number planes in array X
r=zeros(nP,1); c=r; % preallocate output rows,columns arrays
for i=1:nP % iterate over planes of 3D array X
[mn,imn]=min(X(:,:,i),[],'all','linear'); % get minimum, location global minimum over plane of X
[r(i),c(i)]=ind2sub(size(X,1,2),imn); % return row,col indices of minimum in plane
end
This gives the overall minimum location for each plane; by default as your code is written you get the minimums in each column so that is size(X,2) elements but the index is then the row of each column.
That's also convertible to absolute position if that is what is wanted but it isn't clear from the post.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!