필터 지우기
필터 지우기

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
Sherwin
Sherwin 2020년 8월 28일
Thank you so much.

댓글을 달려면 로그인하십시오.

답변 (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