adding cells of two arrays

1 visualizzazione (ultimi 30 giorni)
Sumanth
Sumanth il 1 Mar 2023
Commentato: Stephen23 il 1 Mar 2023
A = [10,9,13,21,18] [9,9,11,18,18]
B = [11,10,12,20,19] [10,9,13,21,18]
How can i add both?
such that C = [21, 19, 25, 41, 37] .....
Operator '+' is not supported for operands of type
'cell'.

Risposte (2)

Star Strider
Star Strider il 1 Mar 2023
Perhaps something like this —
A = {[10,9,13,21,18] [9,9,11,18,18]};
B = {[11,10,12,20,19] [10,9,13,21,18]};
C = cellfun(@(x,y)x+y, A,B, 'Unif',0)
C = 1×2 cell array
{[21 19 25 41 37]} {[19 18 24 39 36]}
This assumes that ‘A’ and ‘B’ are cell arrays, as depicted here.
.

Sumera Zem
Sumera Zem il 1 Mar 2023
A = {[10,9,13,21,18],[9,9,11,18,18]};
B = {[11,10,12,20,19],[10,9,13,21,18]};
C = cell(size(A)); % initialize C with the same size as A
for i = 1:numel(A)
C{i} = A{i} + B{i}; % add the corresponding arrays in A and B
end

Categorie

Scopri di più su Data Types in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by