Abhinav Aggarwal
Abhinav Aggarwal

Reputation: 1363

MATLAB: Vectorize for loop in MATLAB

I wanted to vectorize this piece of code. Is it possible to do this? I tried finding a solution, but I was not able to find any good result on google.

for pos=length1+1:length
    X1(pos) = sim(net1, [demandPred(pos), demand(pos-1), X1(pos-1), X1(pos-2)]')';
    X2(pos) = sim(net1, [demandPred(pos), demand(pos-1), X2(pos-1), X2(pos-2)]')';
end

Thanks in advance. :)

Edit 1:

The model which I am going to simulate is a simple GRNN.

net1 = newgrnn([demand(169:trainElem), demand(169-1:trainElem-1), X1(169 - 1:trainElem - 1), X1(169 - 2:trainElem - 2)]', 0.09);

Upvotes: 0

Views: 119

Answers (1)

Phil Goddard
Phil Goddard

Reputation: 10762

Can Simulink models be vectorized? Sometimes. Can your Simulink model be vectorised? It's impossible to tell without seeing the model -- and how it is being called from m-code (as you've shown in your question) is no indication.

An example of vectorization would be: consider a model with signal s1 that gets added to constant K, and assume that you need to run the models for different values if K. You could use a loop (like the m-code you show) and run the model for each individual required value for K. Alternatively, you can make K a vector, in which case all values would get added to s1 and the result would be a vector of signals s1+K(1), s1+K(2),..., s1+K(n), and the model only needs to be executed once for all of these summations to occur.

But, whether that sort of thing can be done in your model cannot be determined without seeing the model.

Upvotes: 1

Related Questions