How to use vectors instead of loops?

5 views (last 30 days)
Sherwin
Sherwin on 23 Aug 2020
Commented: Stephen23 on 28 Aug 2020
Hi, I am new to coding and I seem to be using too many loops/cells which significantly slows down my codes.
I want to define a 200x1 matrix every cell of which is a 8X17 matrix and the cells of this one are 1x3 vectors. The values in the 1x3 vectors are generated randomly (from different sets of data: Hmin, Hmax, and Wmax are 8x17 matrices of data which have been imported as cell arrays. They are simple numbers which provide a range for the randomly generated values in 1x3 vectors.).
I want to run a genetic algorithm and this would define 200 members of my initial populations. I have tried this in MATLAB using cell arrays; but they significantly slow down my code. This is what I have tried in MATLAB.
I would be grateful if someone would help me to avoid this.
P = cell (200, 1);
for i = 1:200
P{i,1} = cell(8, 17);
for j = 1:8
for k = 1:17
P{i,1}{j,k} = zeros(1,3);
P{i,1}{j,k}(1,1) = randi(Hmax{j}(k));
P{i,1}{j,k}(1,2) = randi([Hmin{j}(k),Hmax{j}(k)]);
P{i,1}{j,k}(1,3) = randi(Wmax{j}(k));
end
end
end
Thank you so much.
  12 Comments

Sign in to comment.

Answers (0)

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!