Summation by using loops and cells

조회 수: 1 (최근 30일)
Jeet kUN
Jeet kUN 2018년 11월 24일
편집: madhan ravi 2018년 11월 24일
K=9;
L Range of M (for each L)
0.5 2 6 8 10 12 14 16
1 2 6 8
1.5 2 6 8 10 12
2 2 6 8 10
2.5 2 6 8 10
3 2 6 8 10 12 14 16
I have a constant parameter say K. Now for different range of L (0.5:0.5:3), I have different values of M, as seen in the table.
I need to carry a summation operation using 'for loops' such that:
sum=(K*0.5*2)+(K*0.5*6)+(K*0.5*8)+.................+ (second line)......+ (sixth line)+ (K*3*16)
[Note: In '(K*0.5*2)' , 0.5 is the first element of L, and 2 is the first value of M for that particular L ]
  댓글 수: 2
madhan ravi
madhan ravi 2018년 11월 24일
upload the datas instead of pic
Jeet kUN
Jeet kUN 2018년 11월 24일
The datas are also uploaded along with the pic. Thanks.

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

채택된 답변

madhan ravi
madhan ravi 2018년 11월 24일
편집: madhan ravi 2018년 11월 24일
Looks nasty although there are efficient solutions which avoids loop:
K=9;
L =0.5:0.5:3;
M{1}=[2 6 8 10 12 14 16];
M{2}=[2 6 8];
M{3}=[2 6 8 10 12];
M{4}=[2 6 8 10];
M{5}=[2 6 8 10];
M{6}=[2 6 8 10 12 14 16];
n=numel(L);
sums=cell(1,n);
for i = 1:n
sums{i}=sum(K.*L(i).*[M{i}]);
end
celldisp(sums) %each cell corresponds to each L and it's M ranges
command window:
>> COMMUNITY
sums{1} =
306
sums{2} =
144
sums{3} =
513
sums{4} =
468
sums{5} =
585
sums{6} =
1836
>>
  댓글 수: 1
Jeet kUN
Jeet kUN 2018년 11월 24일
Hi, apology for the last image attached which didnot showed the various values of M (for a particular L). I need to use 'for loop' here. The updated image is attached.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by