Performing equations on individual elements of a 3D Matrix, and then summing that along the z column

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.

답변 (3개)

A=randi(10,4,3,2) % Example of your matrix result
out=sum(A,3)
Thorsten
Thorsten 2015년 7월 7일
편집: Thorsten 2015년 7월 7일
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);
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

이 질문은 마감되었습니다.

질문:

2015년 7월 7일

마감:

2021년 8월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by