Subtract from a cell array of vectors a vector

2 views (last 30 days)
gsourop
gsourop on 18 Nov 2016
Edited: James Tursa on 18 Nov 2016
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.

Answers (1)

James Tursa
James Tursa on 18 Nov 2016
Edited: James Tursa on 18 Nov 2016
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 Comments
gsourop
gsourop on 18 Nov 2016
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 on 18 Nov 2016
Edited: James Tursa on 18 Nov 2016
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));

Sign in to comment.

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!