简体   繁体   中英

Find min of several columns in matlab without for loop

I have got a matrix M with R rows and C*k columns. I want to create another matrix F with R rows and C columns, so that

 F(:,j) = min(M(:,(j-1)*k+1:j*k),[],2)

In other words first column of F should be min of first k columns of M .

Second column of F should be min of second k columns of M and so on. I am trying to reshape matrix M to R*C xk matrix D find min and reshape it again to R x C matrix.But I cannot properly reshape M to F , so that F(i, :) = M(b, (j-1)*k+1:j*k)

Here is a small example:

k=2;
M = [1 2 3 4; 5 6 7 8; 9 10 11 12;];
then
F = [1 3; 5 7; 9 11]; 

最好的选择是将其重塑为R xkx (C / k)矩阵,然后沿第二维计算min ,然后压缩结果以删除现在为空的第二维。

F = squeeze(min(reshape(M, size(M, 1), k, size(M, 2)/k), [], 2))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM