How do you sum a vector and concatenate the outputs into the original array or vector.

2 views (last 30 days)
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

Answers (2)

Michelangelo Ricciulli
Michelangelo Ricciulli on 14 Mar 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];

KSSV
KSSV on 14 Mar 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

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!