- The line x = pop(i) will extract exactly one element from the variable pop, which makes code on the next line x(:) a bit strange: if there is only one value then it is already a column vector (with one element).
- What is N? Is this a function or an array?
- The variable Tt is defined, but not used. Why?
- Is NF supposed to be the same as Nf?
- What is someFunction? How is it defined?
- The code size(pop(:,1),1) is too complicated: just use size(pop,1) instead.
Vectorization of block consecutive matrixes in a matrix (M = [N(1);N(2)....;N(n)])
1 view (last 30 days)
Show older comments
Hello,
I want to vectorize the following code, but I do not know if this is possible. I have a list with solutions called 'pop'. A solution is a 1x48 vector called 'x'. This 'x' contains indexes to another matrix. Based on this matrix, a fitness value is calculated (f(x)).
At the moment my code looks like the following:
for i = 1:size(pop,1)
x = pop(i,:);
Tt = N(x(:),3:11)'; %Always a [9x48 matrix]
f1 = something using Tt(:,1:6)
f2 = something using Tt(:,1:12)
etc.
Nf = [f1,f2,f3,f4,f5,f6,f7,f8];
f = somefunction(Nf);
end
It is possible to translate the list pop to a matrix [Tt(1);Tt(2);...;Tt(end)] where every 9 rows defines a solution, however due to the calculation of f1,f2 etc, a for loop is still required I think.
Because this is the fitness function of a GA, the speed will greatly improve if it uses vectorization. Is it possible to adapt this code so that it can use vectorization or is it impossible in this case?
With kind regards,
Paul
6 Comments
Stephen23
on 31 Jul 2015
Edited: Stephen23
on 31 Jul 2015
As far as I can tell your original question is about vectorization of a for loop, which although in the question above it starts with for i = 1:size(pop,1) in the code it seems to correlate to %for a = 1:size(thisPopulation,1). Note that in your sample code this line (i.e. the loop itself) is commented-out and that you do not define thisPopulation. Nor do you use the loop variable a, which is only referred to on one line: % A_penalty(a) = penalty; (also commented out).
You state that I "dont need to make any adjustments", in which case the code certainly runs without error and without this for loop. If you do not need the loop then what is your question about? If you do want this loop (or the vectorized equivalent) then how is thisPopulation defined? Or was the loop never actually working?
In any case some observations about your code:
- you only refer to a once inside the loop, so everything before that point can be moved out of the for-loop.
- stop reassigning variables unnecessarily: it just wastes memory and slows things down for no reason. Just stick with one copy of each variable.
- Instead of defining lots of separate variables and then concatenating them together just define these values directly as a row/column of a matrix.
- The loop for i=1:Horizon can probably be vectorized quite easily.
Here is the version that I started to write:
Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!