0
votes

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)
2

2 Answers

1
votes

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.

1
votes

Is this OK?

A(:,floor([1:M]*myFunc())) = A(:,1:M)

coz I don't know if your myFunc is also dependent on i.