Matrix manipulation such as subtraction

조회 수: 1 (최근 30일)
Elvin
Elvin 2013년 12월 19일
댓글: Elvin 2013년 12월 19일
Can I ask for some help regarding matrix manipulation? Here's what I'm after. Let's say for example I have two arrays:
A = [2 3 4 5]
B = [0 1 2 3 4 5 6 7 8 9]
The output that I want is a cell containing an answer like this:
C{1} = [2-0 3-0 4-0 5-0]
C{2} = [2-1 3-1 4-1 5-1]
.
.
.
C{10} = [2-9 3-9 4-9 5-9]

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 12월 19일
편집: Azzi Abdelmalek 2013년 12월 19일
A = [2 3 4 5] ;
B = [0 1 2 3 4 5 6 7 8 9];
out=arrayfun(@(x) A-x,B,'un',0);
celldisp(out)
%or
A = [2 3 4 5];
B = [0 1 2 3 4 5 6 7 8 9];
out1=bsxfun(@minus,A,B')
out=num2cell(out1,2);
celldisp(out)
  댓글 수: 9
Azzi Abdelmalek
Azzi Abdelmalek 2013년 12월 19일
out1={1:6 11:16}
out2={2:7 12:17}
out3={3:8 13:18}
out=cellfun(@(x,y,z) sqrt(x.^2+y.^2+z.^2),out1,out2,out3,'un',0)
Elvin
Elvin 2013년 12월 19일
Thank you very much for the help :D

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

추가 답변 (1개)

José-Luis
José-Luis 2013년 12월 19일
Faster:
your_mat = repmat(A,numel(B),1)-repmat(B',1,numel(A));

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by