MATLAB Answers

Can someone do this calculation without for loops ?

1 view (last 30 days)
Amelos
Amelos on 15 Jun 2016
Edited: Azzi Abdelmalek on 15 Jun 2016
a = [1 2 3; 4 5 6];
b = [ 1 2 3];
for n = 1: size(a,1)
for m = 1:size(a,2)
k(n,m,:)= b.*b*a(n,m)
end
end

  0 Comments

Sign in to comment.

Accepted Answer

José-Luis
José-Luis on 15 Jun 2016
Edited: José-Luis on 15 Jun 2016
k = bsxfun(@times, a , reshape(b.^2,1,1,[]))
alt_k = bsxfun(@times, a , permute(b.^2,[3,1,2]))

  1 Comment

Amelos
Amelos on 15 Jun 2016
this one works also , thanks k = reshape(kron(b.*b,a),[size(a),numel(b.*b)]);

Sign in to comment.

More Answers (2)

Joakim Magnusson
Joakim Magnusson on 15 Jun 2016
Do you mean like this?
fun=@(a,b) b.*b*a
k = bsxfun(fun,a,b)

  0 Comments

Sign in to comment.


Azzi Abdelmalek
Azzi Abdelmalek on 15 Jun 2016
Edited: Azzi Abdelmalek on 15 Jun 2016
a = [1 2 3; 4 5 6];
b = [ 1 2 3];
bb=reshape(b.*b,1,1,[])
out=bsxfun(@times,a,bb)

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!