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

2 次查看(过去 30 天)
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

回答(2 个)

Michelangelo Ricciulli
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 2017-3-14
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

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by