Can someone do this calculation without for loops ?

조회 수: 2 (최근 30일)
Amelos
Amelos 2016년 6월 15일
편집: Azzi Abdelmalek 2016년 6월 15일
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

채택된 답변

José-Luis
José-Luis 2016년 6월 15일
편집: José-Luis 2016년 6월 15일
k = bsxfun(@times, a , reshape(b.^2,1,1,[]))
alt_k = bsxfun(@times, a , permute(b.^2,[3,1,2]))
  댓글 수: 1
Amelos
Amelos 2016년 6월 15일
this one works also , thanks k = reshape(kron(b.*b,a),[size(a),numel(b.*b)]);

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Joakim Magnusson
Joakim Magnusson 2016년 6월 15일
Do you mean like this?
fun=@(a,b) b.*b*a
k = bsxfun(fun,a,b)

Azzi Abdelmalek
Azzi Abdelmalek 2016년 6월 15일
편집: Azzi Abdelmalek 2016년 6월 15일
a = [1 2 3; 4 5 6];
b = [ 1 2 3];
bb=reshape(b.*b,1,1,[])
out=bsxfun(@times,a,bb)

카테고리

Help CenterFile Exchange에서 Dynamic System Models에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by