Filter löschen
Filter löschen

How to use vectors instead of loops?

4 Ansichten (letzte 30 Tage)
Sherwin
Sherwin am 23 Aug. 2020
Kommentiert: Stephen23 am 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 Kommentare
Sherwin
Sherwin am 28 Aug. 2020
Thank you so much.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by