How to use vectors instead of loops?

4 次查看(过去 30 天)
Sherwin
Sherwin 2020-8-23
评论: Stephen23 2020-8-28
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 个评论

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by