How add another loop in program
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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에 대해 자세히 알아보기
참고 항목
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)
