Subtract from a cell array of vectors a vector

조회 수: 2 (최근 30일)
gsourop
gsourop 2016년 11월 18일
편집: James Tursa 2016년 11월 18일
Hi everyone,
I have a cell arrays A of 2x100. Each element inside the cell arrays is a scalar. I need to subtract a 2x1 cell arrays of scalars from the each of column of the cell arrays of A. Do you know how I can do it? I've tried the following but it doesn't work
for t=1:100;
for k=1:2;
C{k,t}(1,1)=(1+A{k,t})-B{k,1}
end;
end;
In addition, do you know how can I subtract from A a vector 2x100 say E? I've tried the following without any success
for t=1:100;
for k=1:2;
D{k}=bsxfun(@minus,(1+A{k,t})',E(t,1));
end;
end;
Thanks in advance.

답변 (1개)

James Tursa
James Tursa 2016년 11월 18일
편집: James Tursa 2016년 11월 18일
Does this do what you want?
A = 2x100 cell array of scalars
B = 2x1 cell array of scalars
C = mat2cell(bsxfun(@minus,cell2mat(A),cell2mat(B)),ones(1,2),ones(1,100));
Or for R2016b you can skip the bsxfun:
C = mat2cell(cell2mat(A)-cell2mat(B),ones(1,2),ones(1,100));
  댓글 수: 2
gsourop
gsourop 2016년 11월 18일
It worked perfect! Thanks a lot! Could you help me please with my second question? do you know how can I subtract from A a vector 1x100 say E? I've tried the following without any success
for t=1:100;
for k=1:2;
D{k}=bsxfun(@minus,(1+A{k,t})',E(t,1));
end;
end;
James Tursa
James Tursa 2016년 11월 18일
편집: James Tursa 2016년 11월 18일
Assuming I understand your question, E is a 1x100 double. So you don't need the cell2mat for that part. Other than that it is pretty much the same. E.g.,
A = 2x100 cell array of scalars
E = 1x100 vector
C = mat2cell(bsxfun(@minus,cell2mat(A),E),ones(1,2),ones(1,100));
or on R2016b you can again skip the bsxfun part:
C = mat2cell(cell2mat(A)-E,ones(1,2),ones(1,100));

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

카테고리

Help CenterFile Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by