Subtract from a cell array of vectors a vector

1 回表示 (過去 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 Center および File ExchangeOperators and Elementary Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by