Performing equations on individual elements of a 3D Matrix, and then summing that along the z column
Infos
Cette question est clôturée. Rouvrir pour modifier ou répondre.
Afficher commentaires plus anciens
I have a 3D matrix of zeros called A, with a select 4x4 area in this set to 1. I then have a for loop where if A=0 an equation is performed and stores, and if A=1 a different equation is performed. Each value needs to be stored so they can be accessed later, and then I also need to produce the sum of each of the z collumns results. I have managed to build my 3D array, and can perform some functions on it, but not that which I am describing.
Réponses (3)
Azzi Abdelmalek
le 7 Juil 2015
A=randi(10,4,3,2) % Example of your matrix result
out=sum(A,3)
You can write the whole computations w/o a for loop in a one-liner. For example, if the equation is A+2 if A == 0 and A.^2 if A == 1, you can write:
B = sum((A==0).*A+2 + (A==1).*A.^2, 3);
Jos (10584)
le 7 Juil 2015
What type of equation are you talking about?
This might get you started
Z = zeros(size(A)) ;
Z(A==0) = 2 % simple equation #1
Z(A==1) = 2*4 % simple equation #2
sum(Z,3) % sum over 3rd dimension, see help sum
Cette question est clôturée.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!