MATLAB - Perform operation on matrix indices without a for loop

五迷三道 提交于 2019-12-25 07:19:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!