How to fill a 3D array with values calculated in a loop?

8 vues (au cours des 30 derniers jours)
Niky Taylor
Niky Taylor le 23 Oct 2017
Commenté : Cedric le 30 Oct 2017
I want to fill a 3D array with values calculated in a loop. Each iteration of the loop generates a single new value which I want to put in an array, so that I end up with a 2D array of all the new values that correspond to their parent value in the old matrix.
D = rand(3,3,4);
d = rand(2,4);
for i=1:3;
for j=1:3;
E = [D(i,j,1);D(i,j,2);D(i,j,3);D(i,j,4)];
F = d*E;
end
end
How do I print each iteration of 'F' into a new array? F is a 2x1 matrix, so I'd like to have one 3x3 array with the F(1,1) values and a second 3x3 array with the F(2,1) values. I'd like to have the arrays separate, I know I can always concatenate them if I need all the data in one place.
I'm also open to suggestions if you think I've gone about this poorly. Thanks in advance.

Réponse acceptée

Cedric
Cedric le 23 Oct 2017
Modifié(e) : Cedric le 23 Oct 2017
This would be one way to do it if you have MATLAB R2016b or newer:
A = sum( D .* permute( d(1,:), [1, 3, 2] ), 3 ) ; % First array.
B = sum( D .* permute( d(2,:), [1, 3, 2] ), 3 ) ; % Second array.
  1 commentaire
Cedric
Cedric le 30 Oct 2017
If this is working for you, please [Accept] the answer. If not, I am happy to discuss what is not working.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Multidimensional Arrays dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by