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.

 Respuesta aceptada

Ameer Hamza
Ameer Hamza el 9 de Mayo de 2020
Editada: Ameer Hamza el 14 de Mayo de 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 comentarios

anindita Roy
anindita Roy el 11 de Mayo de 2020
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?
Ameer Hamza
Ameer Hamza el 11 de Mayo de 2020
Oh! I missed the addition in my original code. Check the updated code. Now you can see the addition too.
anindita Roy
anindita Roy el 11 de Mayo de 2020
Yah. I got it.
Thank you
Ameer Hamza
Ameer Hamza el 11 de Mayo de 2020
I am glad to be of help.
anindita Roy
anindita Roy el 14 de Mayo de 2020
Sorry for a small doubt. where we are using for loop 'j' in terms of 'y' ?
Ameer Hamza
Ameer Hamza el 14 de Mayo de 2020
You are correct. I missed that. The 'j' should appear in the indexing of 'x'. Check the updated code.
anindita Roy
anindita Roy el 18 de Mayo de 2020
thanks.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 9 de Mayo de 2020

Comentada:

el 18 de Mayo de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by