Three Loops with summation

2 ビュー (過去 30 日間)
Ali Kareem
Ali Kareem 2015 年 10 月 24 日
コメント済み: Walter Roberson 2015 年 10 月 24 日
Greeting,
I have matrix Z(30,70) and it repeated for 40 times each time there will be new value for each cell. This means for example Z(1,1) will have 40 different values. And the same for all cells in the matrix. My question how I can sum all 40 Z(1,1) for all values. I want to done this for each cell and save the result in matrix K(30,70)
Do I need to do three loops to accomplish that or there is another ways to do it.
Thank you in advance for all ideas.
Regards
  1 件のコメント
Walter Roberson
Walter Roberson 2015 年 10 月 24 日

サインインしてコメントする。

回答 (2 件)

Walter Roberson
Walter Roberson 2015 年 10 月 24 日
If you have a cell array Z and you want sum each of the entries, then
cellfun(@sum, Z)
if you are expecting a 30 x 70 numeric array as output, and otherwise
cellfun(@sum, Z, 'Uniform', 0)
if you are expecting a 30 x 70 cell array as output.
  2 件のコメント
Ali Kareem
Ali Kareem 2015 年 10 月 24 日
Hello,
Thank you for your reply. Please I do not think my question was clear. Below what I want to do
for L=1:10
for i=1:20
for j=1:100
r=(Xc+Y)^(1/2);
VC=VCo*(1-(exp(-(r))));
Uf(i,j)=(((VC-Yc))/((pi)*(r)));
Vf(i,j)=((VC*r)*(Xc))/((2*pi));
Uft(i,j)=Uft(i,j)+Uf(i,j);
Vft(i,j)=Vft(i,j)+Vf(i,j);
Ufz(i,j)=(U-Uft(i,j))/U;
end;
end;
end;
I Want to have summation of Uft and Vft. so for the entire loops (i,j) I will get the first matrix (20,100) then after that for L loop I will get the other matrices. I want Uft(1,1) to add to the Uft(1,1) from old matrix until end of loop L (this mean for ten times)
this scenario for for new A(1,2)=old A(1,2) and so on
Walter Roberson
Walter Roberson 2015 年 10 月 24 日
for L=1:10
for i=1:20
for j=1:100
r=(Xc+Y)^(1/2);
VC=VCo*(1-(exp(-(r))));
Uf(i,j)=(((VC-Yc))/((pi)*(r)));
Vf(i,j)=((VC*r)*(Xc))/((2*pi));
Uft(i,j,L)=Uft(i,j,L)+Uf(i,j);
Vft(i,j,L)=Vft(i,j,L)+Vf(i,j);
Ufz(i,j)=(U-Uft(i,j))/U;
end;
end;
end;
Now afterwards you can use
sum(Utf,3)
sum(Vtf,3)

サインインしてコメントする。


Matt J
Matt J 2015 年 10 月 24 日
編集済み: Matt J 2015 年 10 月 24 日
You should be keeping your Z data as a 3D array of dimension 30x70x40. Then, it's as simple as
K=sum(Z,3);
Example,
>> Z=rand(30,70,40); K=sum(Z,3); whos K Z
Name Size Bytes Class Attributes
K 30x70 16800 double
Z 30x70x40 672000 double
  1 件のコメント
Ali Kareem
Ali Kareem 2015 年 10 月 24 日
編集済み: Walter Roberson 2015 年 10 月 24 日
Hello,
Thank you for your reply.
for L=1:10
for i=1:20
for j=1:100
r=(Xc+Y)^(1/2);
VC=VCo*(1-(exp(-(r))));
Uf(i,j,L)=(((VC-Yc))/((pi)*(r)));
Uft= sum(Uf,3)/U
end;
end;
end;
I think my equation for sum is incorrect because I got wrong numbers
Regards

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by