Subtract from a cell array of vectors a vector
조회 수: 2 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
답변 (1개)
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
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 Center 및 File Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!