Here is my question :
I have A = 1xN matrix and B = 6xN matrix
I also have a function F = @(x,y) …
What I want to do is to loop over the values of A and B in a way that each time the input arguments for F will be : A(1,i) and B(:,i)
Is there a way to that for all values without using a for loop in order to gain some time?
Cheers
With newer versions of MATLAB loops aren’t as costly as you may think. If your function
Fis something relatively simple that can be easily vectorized then you can usually get faster performance than using a for loop, but ifFrepresents a rather complex operation there may actually be more work required in trying to get around using a for loop.Without knowing what
Factually does, it’s difficult to give you a specific answer. However, in addition to vectorizing your operations, there are a few functions that can be used in place of for loops for certain situations. Some examples of these would be ARRAYFUN, CELLFUN, STRUCTFUN, and BSXFUN.As an example of how to use BSXFUN, let’s say that the operation
Fthat you are performing involves multiplying each column ofBby the value in the corresponding column ofA. In other words,C(:,i) = A(1,i).*B(:,i);for alli. You could do this using BSXFUN as follows: