How add another loop in program
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
Hi
First of all, thanks to all the experts who help others...I appreciate your time, And best wishes to everyone.
I have program ( take column1 column2 until column100 , to get 100 a and 100 b and 100 c)and I need to get the following...which print it as follows ,and I dont know where I add it in the same program since it depend on the output of it (that get 100 values of a ,b,c ).where A,B, C are the intial values.

c2=0;
for w=1:100
c2=c2+1;
value1=value1+(((a(w))-A)^2)\100; %a(w) is 100 value of a
value2=value2+(((b(w))-B)^2)\100; %b(w) is 100 value of b
value3=value3+(((c(w))-C)^2)\100; %c(w) is 100 value of c
end
value1;
value2;
value3;
and, (if possible) to add to the program" if else "for the error (or "while " of "if" as you show correct in my program)
if any Prof. can help me thanks alot.
2 件のコメント
I donot know how I get the values of a,b,c that obtained inside every loop .
and put it as 100 values in the output as a 100 column to save it all ,, what I change in the program..please
Jan
2021 年 3 月 19 日
I do not exactly understand, what you want to change.
採用された回答
Jan
2021 年 3 月 19 日
The only problems I see is that value1/2/3 is not initialized and that the division is /, not \ .
c2 is not used, so omit it.
value1 = 0;
value2 = 0;
value3 = 0;
for w = 1:100
value1 = value1 + (a(w) - A)^2 / 100;
value2 = value2 + (b(w) - B)^2 / 100;
value3 = value3 + (c(w) - C)^2 / 100;
end
value1
value2
value3
Or vectorized as typical Matlab code:
value1 = sum((a - A)^2) / 100;
value2 = sum((b - B)^2) / 100;
value3 = sum((c - C)^2) / 100;
14 件のコメント
thanks for your reply, really I donot know that "the division is / not \ " I thought it is the same
how can I used the values of a,b,and c in your Excellent programming.
the values exist in attach file , whose in every loop calculater one value for a and one value for b ,and one value for c , and so on to get 100 values...I donot know how save the 100 values of a and 100 values of b and 100 values of c , to put it in your programming, please
In your attached m-File, I assume this line contains a typo:
sum5=sum7+log((Q(i,j))/A);
% ^ should this be a 5?
c1 and s are not used, so simply omit them.
A simplified version of your code:
Q = unifrnd(0,1,10,100); % matrix 10*100
A = 1;
B = 2;
C = 3;
for j = 1:100
sum1 = sum(exp(A*(1-exp((Q(:, j) / A).^C))));
sum2 = sum(exp(C*A*(1-exp((Q(:,j) / A).^C))) .* ...
exp((Q(i,j) / A).^C));
sum3 = sum(exp(B*A*(1-exp((Q(:,j) / A).^C))) .* ...
(Q(i,j) / A).^C .* log(Q(i,j) / A).^2);
sum4 = sum(exp(2*A*B*(1-exp((Q(:,j) / A).^C))));
sum5 = sum(log(Q(:,j) / A));
x = real([-2*A*sum1+2*sum2;C*sum3+A*sum4;-2*A*sum5]);
y1 = -sum1;
y2 = -sum2;
y3 = -B*sum3;
y4 = -sum2;
y5 = -sum4;
y6 = -B*A*sum5;
y7 = -B*sum3;
y8 = -B*A*sum5;
y9 = C*sum1;
d = real([y1 y2 y3; y4 y5 y6; y7 y8 y9]);
z0 = [A; B; C];
p = d \ x;
z = z0-p;
a = z(1,1); % Maybe all you want is: a(j) = ...
b = z(2,1); % b(j) = ...
c = z(3,1);
err=abs(z-z0);
% if any (err>[0.00001;0.00005;0.00002]) then stop ( I don not know how err choose to continue untill 100 column , sinec I donot want to stop untill get take 100 j and in exam the questions are different)
%if not continue with another j
end %to loop j
I do not undestand the part with err: You want to stop what? An iteration until err matchs specific limits would not be useful here, because the core of the loop operates on constant inputs, so a next iteration would give you the same result.
hasan s
2021 年 3 月 19 日
thank you very much for your help
yes thats right sum5.
I used c1 and s .. to ensure that the program take all j=1:100 and to ensure that the program take all i=1:10
yes I want is: a(j) = which is the first row of z = z0-p; in the program
b(j) = which is the second row of z = z0-p;in the program
c(j) = which is the third row of z = z0-p;in the program
but when I write a(j)=z(1,1) , I will obtain error, how wright it please?
pardon , you said, "Next iteration will give you the same result." How do I get the same result while taking another column that differs from the first every time.
Jan
2021 年 3 月 19 日
Why to you calculate err, if you do not use it?
but when I write a(j)=z(1,1) , I will obtain error, how wright it please?
Please post a copy of the error message. You have this valuable information already, so sharing it with the readers helps to solve your problem.
hasan s
2021 年 3 月 19 日
I want to add the error condition as a form only in this position(because I am required to add it) so that it does not affect the work of the program, i.e. it keeps reading column 1 and then reading column 2 and calculating the result until it reaches 100 columns
hasan s
2021 年 3 月 19 日

hasan s
2021 年 3 月 19 日
thanks alot for your help ..please prof. Jan
Where I add your programming.
value1 = sum((a - A)^2) / 100;
value2 = sum((b - B)^2) / 100;
value3 = sum((c - C)^2) / 100;
to the simplified version of code, please.
please , is Q(:,j) Will it take all rows for the column?
Without writing the number of rows is 10?
Yes, Q(:, j) is the j.th column. Try it:
a = magic(4); % or rand(4), any matrix
a(:, 3)
The iterativ growing of an array is not an error, but rather inefficient:
x = [];
for k = 1:100
x(k) = rand + k;
end
This does not reserve memory for 100 elements, but in each iteration a new vector is reserve. Then finally Matlab has to obtain memory for sum(1:100) elements, which is much more than the final vector has. Solution: Pre-allocation:
x = zeros(1, 100);
for k = 1:100
x(k) = rand + k;
end
Do this for a,b,c before the loop also.
I assume, you can append this at the bottom of the code:
value1 = sum((a - A).^2) / 100;
value2 = sum((b - B).^2) / 100;
value3 = sum((c - C).^2) / 100;
hasan s
2021 年 3 月 19 日
thanks alot
please I try it , the output error in
Error using ^ (line 51)
Incorrect dimensions for raising a matrix to a power. Check that the matrix is square and the power is a scalar. To
perform elementwise matrix powers, use '.^'
Error in (line 9)
sum1 = sum(exp(A*(1-exp((Q(:,j)/ A)^C))));
hasan s
2021 年 3 月 19 日
I put .* and .^ and run the program , but the output are 100 number of a , and 100 numer of a, and so on to 100 times contain 100 numbers
while I need only 100 numbers of a , may be because of a(j) = z(1,1) .since
when I put a = z(1,1) , I get in every loop one numbers ,,,thus the output must 100 numbers only... but I canot put it as one column that contain 100 numbers ,,, in order to apply
value1 = sum((a - A)^2) / 100;
value2 = sum((b - B)^2) / 100;
value3 = sum((c - C)^2) / 100;
what I do please?
hasan s
2021 年 3 月 19 日
is every * put .* in the program?in y1,...y9 not put .* on its equations? is correct ? can you show it please?
the first my program not need that ?or I had mistake on it also ?
hasan s
2021 年 3 月 19 日
is
a = zeros(1, 100);
b = zeros(1, 100);
c = zeros(1, 100);
Make the result repeat 100 times and each time contains 100 numbers ?? ? please , Can you replace it with something else?
thank you for your help
I had some mistakes in my suggested code: the elementwise operations .* and .^ are required.
Make the result repeat 100 times and each time contains 100 numbers ?
No, this pre-allocation let a,b,c be vectors of size [1, 100].
Prof Jan...
Iam sorry ....the repeat of program alot of times due to damage of matlab in my laptop.
Now it is running correctly.
thank you very very much for your help
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Creating and Concatenating Matrices についてさらに検索
参考
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
