How to get the smallest value in submatrices

1 vue (au cours des 30 derniers jours)
Hung Dao
Hung Dao le 2 Oct 2021
Commenté : Hung Dao le 2 Oct 2021
Hi,
I would appreciate if you could help me with this problem.
I have a big matrix , where are submatrices of the same size.
I want to get a vector containing the smallest elements in each submatrices. I want to avoid for loop as it might slow down my whole program.
Thanks.

Réponse acceptée

Bruno Luong
Bruno Luong le 2 Oct 2021
B=randi(9,10,2)
B = 10×2
3 7 6 6 6 3 3 5 3 9 3 5 1 5 5 9 8 2 7 8
[m,n] = size(B);
k = 5;
if mod(m,k)
error('m must divisible by k');
end
minsub = min(reshape(B,[m/k k n]),[],[1 3]).'
minsub = 5×1
3 3 3 1 2
  1 commentaire
Hung Dao
Hung Dao le 2 Oct 2021
Thank you very much.
This is exactly what I need.

Connectez-vous pour commenter.

Plus de réponses (1)

Kevin Holly
Kevin Holly le 2 Oct 2021
cellfun(@min,B,'UniformOutput',false)
%or
cellfun(@min,cellfun(@min,B,'UniformOutput',false),'UniformOutput',false)
  6 commentaires
Kevin Holly
Kevin Holly le 2 Oct 2021
I was under the impression that you already had a big matrix B that contained submatrices. If this is not the case, how are your data organized? Are all the matrices in a folder/workspace individually?
Hung Dao
Hung Dao le 2 Oct 2021
My apologies for the confusion.
My algorithm leads to a big matrix, not the submatrices.
But I need to partition the big matrix into submatrices of the same size, and in each submatrix I want to find the minimum value.
I hope it is clear now.
Thank you.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by