Can I change iteration value in following for loop?

2 vues (au cours des 30 derniers jours)
Jake
Jake le 21 Déc 2022
Commenté : Jan le 22 Déc 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!

Réponse acceptée

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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Schedule Model Components dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by