Filter löschen
Filter löschen

adding cells of two arrays

5 Ansichten (letzte 30 Tage)
Sumanth
Sumanth am 1 Mär. 2023
Kommentiert: Stephen23 am 1 Mär. 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'.

Antworten (2)

Star Strider
Star Strider am 1 Mär. 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 am 1 Mär. 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

Kategorien

Mehr zu Data Types finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by