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

1 visualización (últimos 30 días)
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

Respuestas (2)

Michelangelo Ricciulli
Michelangelo Ricciulli el 14 de Mzo. de 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 el 14 de Mzo. de 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

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by