필터 지우기
필터 지우기

adding every element of an array with every element of another array?

조회 수: 11 (최근 30일)
Pranav
Pranav 2012년 11월 30일
편집: Brandon 2018년 12월 11일
dear all, i want to add array elements to every other element of another element of another array in simulink/matlab

채택된 답변

Andrei Bobrov
Andrei Bobrov 2012년 11월 30일
Pranav wrote: "...i want to add elemnts of A to be added to every other elemnet of B. ie. 1+[5 6 7 8],2+[5 6 7 8],...so on..."
A = [ 1 2 3 4];
B = [ 5 6 7 8];
C = reshape(bsxfun(@plus,A,B.'),1,[]);

추가 답변 (3개)

Vishal Rane
Vishal Rane 2012년 11월 30일
편집: Vishal Rane 2012년 11월 30일
Do you need to do this in MATLAB code ?
A = [ 1 2 3 4]
B = [ 5 6 7 8]
C = A + B = [ 6 8 10 12 ]
Comment back if your requirement is different.
  댓글 수: 3
Vishal Rane
Vishal Rane 2012년 11월 30일
arrayfun(@(x)(x+A),B,'Un',0)
will work in that case but output will be a 1x4 cell arrray.
Pranav
Pranav 2012년 12월 3일
Thanks Vushal i will check for other array sizes.

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


Matt Fig
Matt Fig 2012년 11월 30일
A = 0:4;
B = 10:16;
bsxfun(@plus,A,B')
  댓글 수: 1
Pranav
Pranav 2012년 12월 3일
Thanks u Matt fot the solution. I shall convert it into a row vector.

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


Brandon
Brandon 2016년 5월 9일
편집: Brandon 2018년 12월 11일
I found this conversation while looking for a faster way to accomplish this task. The code below also works but is just slightly slower in my code on my machine.
X=[A' ones(size(A'))];
Y=[ones(size(B)); B];
reshape(X*Y,1,[])

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by