how can i simplify this expression

1 view (last 30 days)
u is 37x37x37x37 complex matrix
h is 37*37 complex matrix
i want to simplify this expression:
N=37;
res=zeros(N,N,N,N);
for i=1:N
for j=1:N
res(:,:,i,j)=h(i,j)*u(:,:,i,j);
end
end
res=sum(res,[3 4]);
  1 Comment
Ameer Hamza
Ameer Hamza on 8 Apr 2020
I guess the for loop is already the simplest way you can do it.

Sign in to comment.

Accepted Answer

David Goodmanson
David Goodmanson on 9 Apr 2020
Hi Dror,
n1 = 10; % in case the dimensions are not all the same
n2 = 6;
n3 = 33;
n4 = 28;
u = rand(n1,n2,n3,n4) + i*rand(n1,n2,n3,n4);
h = rand(n3,n4) + i*rand(n3,n4);
res = zeros(n1,n2,n3,n4);
for ii = 1:n3 % I don't use i as a sum variable so i can stay as sqrt(-1)
for j = 1:n4
res(:,:,ii,j)=h(ii,j)*u(:,:,ii,j);
end
end
res = sum(res,[3 4])
% different way
uu = reshape(u,n1*n2,n3*n4);
hh = reshape(h,n3*n4,1);
res1 = reshape(uu*hh,n1,n2) % same thing

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!