loop for sum in matrix

조회 수: 2 (최근 30일)
Mingde
Mingde 2022년 7월 20일
편집: Mingde 2022년 7월 20일
Hello
I want to using loop for do this work. matrix shape(16x10)
converting sum matrix from 2000->200->5 (5x40x10)
i have M1 = [1 2 3 4 .... 2000]
i want to sum each 10 index so the total index is 200, M2=[1+2+3+...+10, 11+12+...20, 21+22+23+...+30, ... ,1991+1992+...2000]
So M3= [55, 155, 255,....+19955]
and I want to sum each 40 index so the total index is 5, M4=[.... , ..... ,.... ,.... ,.... ]
one more thing, I want to make matrix overlab
from 2000->200->40->5
  댓글 수: 2
KSSV
KSSV 2022년 7월 20일
Read about reshape
Mingde
Mingde 2022년 7월 20일
편집: Mingde 2022년 7월 20일
I know about reshape(I don't want to change shape), but in this work is using for loop.

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

채택된 답변

SAI SRUJAN
SAI SRUJAN 2022년 7월 20일
Hi Mingde,
From my understanding of your question,you want to find m4 using for loops.The following code might help,
m1=1:2000;
m2=[];
sum=0;
for i=1:2000
sum=sum+m1(i);
if rem(i,10)==0
m2=[m2 sum];
sum=0;
end
end
m4=[];
for i=1:200
sum=sum+m2(i);
if rem(i,40)==0
m4=[m4 sum];
sum=0;
end
end
  댓글 수: 1
Mingde
Mingde 2022년 7월 20일
ok, let's me try this code.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by