Find the indices of minimum value in each block of a block diagonal matrix
3 views (last 30 days)
Show older comments
I have a 1164x1170 matrix that is mostly NaNs except for blocks of values on the diagonal. They are not in blocks of predictable size.
I want to know the indices of the minimum value in each block. I wanted to use mat2cell but I don't know the size of the blocks ahead of time.
Edit: I have attached my data matrix as a .mat - also a version of it with zeros instead of NaNs
4 Comments
Stephen23
on 29 Jun 2023
"I guess I could do a for loop over this number with the labels to try and find the minima?"
That is what my code does. Did you try it?
Answers (1)
Stephen23
on 29 Jun 2023
Edited: Stephen23
on 29 Jun 2023
Using BWLABEL as KSSV suggested:
S = load('synced_stamped.mat');
M = S.synced_stamp
M(isnan(M)) = 0;
[X,N] = bwlabel(M,4)
F = @(n)myfun(M,n==X);
Z = arrayfun(F,1:N) % linear indices
[R,C] = ind2sub(size(M),Z) % optional subscript indices
function Z = myfun(M,X)
[~,I] = min(M(X));
Y = find(X);
Z = Y(I);
end
0 Comments
See Also
Categories
Find more on Operating on Diagonal Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!