简体   繁体   English

Matlab:从一组值中为数组中的每一列查找最小值

[英]Matlab: Find minimum value from a set of values for each column in an array

I have a 2-D array A=zeros(1000,1024) . 我有一个二维数组A=zeros(1000,1024) I want to iteratively compute the difference between each of the value of the i-th row (with i=1-999) and the values of 1000th row. 我想迭代计算第i行的值(i = 1-999)与第1000行的值之间的差。

Right now I think of looping over the 1-999 rows, compute the differences of the current row and the 100th row and store it in a seperaate data structure ( B=zeros(999,1024) ). 现在,我想遍历1-999行,计算当前行与第100行的差,并将其存储在单独的数据结构中( B=zeros(999,1024) )。 After, I compute the minimum of each column using another for-loop, which iterates over the columns of B . 之后,我使用另一个for循环计算每个列的最小值,该循环遍历B的列。

Do you know of a more efficient, faster approach? 您知道更有效,更快捷的方法吗?

如果只希望每列的最小值,那么可以通过在最后进行减法来节省许多操作:

min(A(1:end-1,:),[],1) - A(end,:)

Try this - 尝试这个 -

min(bsxfun(@minus,A(1:999,:),A(1000,:)),[],1)

It seems you want to subtract from the last row, so you can make it general - 看来您想从最后一行中减去,因此可以将其设为通用-

min(bsxfun(@minus,A(1:end-1,:),A(end,:)),[],1)

This is a classic use case for bsxfun : 这是bsxfun的经典用例:

M = rand(1000,1024);
V = M(end,:);

MminusV = bsxfun(@minus, M(1:end-1,:), V);

min(MminusV)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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