필터 지우기
필터 지우기

How to sum multiple rows elements within the matrix

조회 수: 6 (최근 30일)
Abdulhakim Alezzi
Abdulhakim Alezzi 2020년 3월 13일
댓글: Abdulhakim Alezzi 2020년 3월 13일
I have a matrix with (30*70 ).
Does anybody know how I can calculate the the average of every 3 rows together?
for example : rows from (1-3) to be 1 row and second row to be the average of (4-6) and so on .
the final matrix must be 10*70

채택된 답변

Sriram Tadavarty
Sriram Tadavarty 2020년 3월 13일
Hi Abdulhakim,
The mean and reshape functions are helpful for your case. Here is the code that does what is asked for.
% Consider the input matrix is a
a = rand(30,70);
% Reshape the matrix such a way you have only 3 rows
aReshape = reshape(a,3,[]); % This turns up to 3 x 700
% Use the mean function
avg = mean(aReshape); % This returns the average of 3 elements in each column (1 x 700)
% Now to get the desired ouput in matrix form, perform the reshape again
out = reshape(avg,[],70); % Output size is (10 x 70)
The links to the document page of mean and reshape functions are:
Hope this helps.
Regards,
Sriram
  댓글 수: 1
Abdulhakim Alezzi
Abdulhakim Alezzi 2020년 3월 13일
Dear Sriram , That is very helpfull, it workd perfectly.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Fangjun Jiang
Fangjun Jiang 2020년 3월 13일
a=rand(6,7);
b=reshape(a,3,[]);
c=mean(b);
d=reshape(c,2,[]);
  댓글 수: 1
Abdulhakim Alezzi
Abdulhakim Alezzi 2020년 3월 13일
Dear Fangjun,
Thank you very much. the concept is very correct, but the output was a matrix of 2x150.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by