How do you sum a vector and concatenate the outputs into the original array or vector.
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
function[ArrayZ]=SumArray(ArrayG)
A=[1 4 3]
B=[6 8 9]
ArrayZ=[A;B]
S=sum(ArrayZ,1) %%outputs the sum of the colm
S=sum(ArrayZ,2)%%outputs the sum of the row
%%I want to concantinate? to have ArrayZ=[1 4 2 8;
6 8 9 23;
7 12 12 31]
%%How do I merge the array with the sum outputs?
endfunction
Antworten (2)
Michelangelo Ricciulli
am 14 Mär. 2017
I think this is the solution:
S=sum(ArrayZ,1) %%outputs the sum of the colm
ArrayZ=[ArrayZ; S]; %%append the sums
S=sum(ArrayZ,2)%%outputs the sum of the row (included appended new values)
ArrayZ=[ArrayZ, S];
0 Kommentare
KSSV
am 14 Mär. 2017
function[ArrayZ]=SumArray(ArrayG)
A=[1 4 3] ; B=[6 8 9] ;
AB=[A;B] ;
S1=sum(AB,1) ;%%outputs the sum of the colm
S2=sum(AB,2) ;%%outputs the sum of the row
ArrayZ = AB ;
ArrayZ(:,end+1) = S2 ;
ArrayZ(end+1,:) = [S1 sum(S2)] ;
end
0 Kommentare
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!