3D matrix generic loop calculation

Dear sir/ma'am
I have a 3D matrix as
y(:,:1)=h(:,:,1)*x(:,:,1)+h(:,:,2)*x(:,:,2)+h(:,:,3)*x(:,:,3)+h(:,:,4)*x(:,:,4)
y(:,:2)=h(:,:,5)*x(:,:,1)+h(:,:,6)*x(:,:,2)+h(:,:,7)*x(:,:,3)+h(:,:,8)*x(:,:,4)
y(:,:3)=h(:,:,9)*x(:,:,1)+h(:,:,10)*x(:,:,2)+h(:,:,11)*x(:,:,3)+h(:,:,12)*x(:,:,4)
y(:,:1)=h(:,:,13)*x(:,:,1)+h(:,:,14)*x(:,:,2)+h(:,:,15)*x(:,:,3)+h(:,:,16)*x(:,:,4)
But I want to compute generic matlab code as a loop for this implementation such that I can operate any value of h,x,y matrix and reach the same result as above description.
Thank you.

 Risposta accettata

Ameer Hamza
Ameer Hamza il 9 Mag 2020
Modificato: Ameer Hamza il 14 Mag 2020
Something like this
h = rand(5, 5, 16);
x = rand(5, 5, 4);
y = zeros(size(x));
count = 1;
for i=1:size(x, 3)
for j=1:4
y(:,:,i) = y(:,:,i) + h(:,:,count)*x(:,:,j);
count = count + 1;
end
end

7 Commenti

Thank you for responding.
In this code I have understand about the multipliaction part for multiplication with 'h' and 'x' also it is working properly. But here I am not able to undertand that how the addition principle is working?
Oh! I missed the addition in my original code. Check the updated code. Now you can see the addition too.
Yah. I got it.
Thank you
I am glad to be of help.
Sorry for a small doubt. where we are using for loop 'j' in terms of 'y' ?
You are correct. I missed that. The 'j' should appear in the indexing of 'x'. Check the updated code.
thanks.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by