I have a 2-D array 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.
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)
). After, I compute the minimum of each column using another for-loop, which iterates over the columns of 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
:
M = rand(1000,1024);
V = M(end,:);
MminusV = bsxfun(@minus, M(1:end-1,:), V);
min(MminusV)
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.