필터 지우기
필터 지우기

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

조회 수: 3 (최근 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
Michelangelo Ricciulli 2017년 3월 14일
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