问题
It's been a little while since I've done matrix operations in MATLAB, so please forgive me if this is easily solvable.
I have some NxM matrix A
, and I would like to perform an operation on the column indices of A
. I know how to do this using a for
loop, but since I'm using MATLAB I'd like to make use of MATLAB's ability to do operations on matrices fast.
Suppose I have a function called myFunc
. Is there a way to do the following without a for
loop (such as with matrix multiplication):
for i=1:M
A(:, floor(i*myFunc())) = A(:, i)
回答1:
You can probably just replace i*
by (1:M).*
, like this:
A(:, floor((1:M).*myFunc())) = A(:,1:M)
Note that .*
does element-wise multiplication instead of matrix multiplication.
回答2:
Is this OK?
A(:,floor([1:M]*myFunc())) = A(:,1:M)
coz I don't know if your myFunc is also dependent on i.
来源:https://stackoverflow.com/questions/15163814/matlab-perform-operation-on-matrix-indices-without-a-for-loop