필터 지우기
필터 지우기

Can I change iteration value in following for loop?

조회 수: 2 (최근 30일)
Jake
Jake 2022년 12월 21일
댓글: Jan 2022년 12월 22일
Suppose I have the following code.
nf1 = 3; nf2 = 4;
panel_no = 1:12;
R_vx = rand(144,1);
for i = 1:nf1
for ii = 1:nf2
for iii = 1 : length(panel_no)
velx(iii,:, ii, i) = [panel_no(iii), R_vx(iii)];
end
end
end
velx
My goal is to create a 4D double matrix, and velx provides the right structure. However, it only includes the first 12 values of R_vx (for obvious reasons, of course). If the second iteration of the for loop starts from 13 for R_vx, and the third one from 25 and so on, I'd get the desired matrix. Is there a way to implement this? TIA!

채택된 답변

Jan
Jan 2022년 12월 21일
Maybe you mean:
nf1 = 3; nf2 = 4;
panel_no = 1:12;
R_vx = rand(144,1);
velx = repmat([panel_no.', reshape(R_vx, 12, 12)], 1, 1, nf1, nf2);
It is hard to guess the wanted output based on a not working code.
  댓글 수: 6
Jake
Jake 2022년 12월 22일
Yes! The bold guess works! :)
Introducing c was the missing link. Thank you so much!
Jan
Jan 2022년 12월 22일
@James: Fine. Then without a loop:
nf1 = 3; nf2 = 4;
panel_no = 1:12;
R_vx = rand(144,1);
A = [repmat(panel_no, 1, numel(R_vx)/numel(panel_no)); R_vx.'];
B = reshape(A, 2, 12, 4, 3);
C = permute(B, [2, 1, 3, 4]);

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Schedule Model Components에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by