Filter löschen
Filter löschen

Can I change iteration value in following for loop?

2 Ansichten (letzte 30 Tage)
Jake
Jake am 21 Dez. 2022
Kommentiert: Jan am 22 Dez. 2022
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!

Akzeptierte Antwort

Jan
Jan am 21 Dez. 2022
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 Kommentare
Jake
Jake am 22 Dez. 2022
Yes! The bold guess works! :)
Introducing c was the missing link. Thank you so much!
Jan
Jan am 22 Dez. 2022
@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]);

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Schedule Model Components 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