Subtract matrices in an array 'A' with elements from matrix 'B'

17 ビュー (過去 30 日間)
Leeba Chacko
Leeba Chacko 2021 年 3 月 11 日
回答済み: Leeba Chacko 2021 年 3 月 11 日
I have a 1x27 cell array 'A' containing 27 [56x2] doubles. I also have a 27x2 matrix 'B'. I want to subtract all the elements from A{1} with B(1,:) and A{2} with B(2,:) and so on. How do I write a for loop for this?

採用された回答

Leeba Chacko
Leeba Chacko 2021 年 3 月 11 日
Thank you for the answers. I managed to solve this using the following code.
[m n]=size(B);
for i=1:length(A)
for j=1:m
D{i}(j,:)= A{i}(j,:) - B(i,:);
end
end

その他の回答 (2 件)

KSSV
KSSV 2021 年 3 月 11 日
iwant = cell(size(A));
for i = 1:length(A)
iwant{i} = A{i}-B(i,:) ;
end

Walter Roberson
Walter Roberson 2021 年 3 月 11 日
%generate A and B for demo
A = arrayfun(@(idx) rand(56,2), 1:27, 'uniform', 0)
A = 1x27 cell array
{56×2 double} {56×2 double} {56×2 double} {56×2 double} {56×2 double} {56×2 double} {56×2 double} {56×2 double} {56×2 double} {56×2 double} {56×2 double} {56×2 double} {56×2 double} {56×2 double} {56×2 double} {56×2 double} {56×2 double} {56×2 double} {56×2 double} {56×2 double} {56×2 double} {56×2 double} {56×2 double} {56×2 double} {56×2 double} {56×2 double} {56×2 double}
B = rand(27,2)
B = 27×2
0.5867 0.3973 0.6645 0.7897 0.1848 0.7124 0.1926 0.5554 0.5047 0.3560 0.6562 0.5289 0.7456 0.6495 0.1655 0.6155 0.0578 0.6535 0.4604 0.5884
%now for the work
C = cellfun(@(a,b) a-b, A, num2cell(B, 2).', 'uniform', 0)
C = 1x27 cell array
{56×2 double} {56×2 double} {56×2 double} {56×2 double} {56×2 double} {56×2 double} {56×2 double} {56×2 double} {56×2 double} {56×2 double} {56×2 double} {56×2 double} {56×2 double} {56×2 double} {56×2 double} {56×2 double} {56×2 double} {56×2 double} {56×2 double} {56×2 double} {56×2 double} {56×2 double} {56×2 double} {56×2 double} {56×2 double} {56×2 double} {56×2 double}

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by