How to use vectors instead of loops?

5 visualizaciones (últimos 30 días)
Sherwin
Sherwin el 23 de Ag. de 2020
Comentada: Stephen23 el 28 de Ag. de 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 comentarios
Sherwin
Sherwin el 28 de Ag. de 2020
Thank you so much.

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by