calculating each elements of matrixs

Hi all I am a highschool student studying matlab
What i want is to prgramming C matrix if matrix A (nx2), matrix B (nx2)
C(1,:)=A(1,:)-B(1,:)
C(2,:)=A(2,:)-B(1,:)
C(3,:)=A(3,:)-B(1,:)
C(4,:)=A(4,:)-B(1,:)
:
:
C(n,:)=A(n,:)-B(1,:)
C(n+1,:)=A(n,:)-B(2,:)
C(n+2,:)=A(n,:)-B(2,:)
C(n+3,:)=A(n,:)-B(2,:)
C(N+4,:)=A(n,:)-B(2,:)
:
:
C(n*n,:)=A(n,:)-B(n,:)
I ve tried like
I=1;
J=1;
K=1;
C(I,:) = A(J,:)-B(K,:);
for J = J+1
K = K+1
I = I+1
end
But failed, I hope some of you might help me

댓글 수: 1

James Tursa
James Tursa 2012년 5월 19일
Cross-posted on newsgroup. Looks like the last A(n,:) part is a repeated typo to me and should be
C(n+1,:)=A(1,:)-B(2,:)
C(n+2,:)=A(2,:)-B(2,:)
C(n+3,:)=A(3,:)-B(2,:)
etc.

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

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2012년 5월 19일

0 개 추천

C = reshape(bsxfun(@minus,A',permute(B',[1 3 2 ])),size(A,2),[])'
or
C = reshape(bsxfun(@minus,permute(A,[1 3 2]),permute(B,[3 1 2 ])),[],2)
or
C = reshape(bsxfun(@minus,reshape(A,size(A,1),1,[]),reshape(B,1,size(A,1),[])),[],2)

추가 답변 (1개)

tizh
tizh 2012년 5월 20일

0 개 추천

Thank s it helped a lot :)

카테고리

도움말 센터 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2012년 5월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by