Subtract from a cell array of vectors a vector
1 次查看(过去 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 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!