How add another loop in program

1 次查看(过去 30 天)
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 个评论
hasan s
hasan s 2021-3-18
编辑:hasan s 2021-3-18
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
Jan 2021-3-19
I do not exactly understand, what you want to change.

请先登录,再进行评论。

采纳的回答

Jan
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 个评论
Jan
Jan 2021-3-20
编辑:Jan 2021-3-22
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].
hasan s
hasan s 2021-3-20
编辑:hasan s 2021-3-21
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 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by