How add another loop in program

1 vue (au cours des 30 derniers jours)
hasan s
hasan s le 18 Mar 2021
Modifié(e) : Jan le 22 Mar 2021
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 commentaires
hasan s
hasan s le 18 Mar 2021
Modifié(e) : hasan s le 18 Mar 2021
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 le 19 Mar 2021
I do not exactly understand, what you want to change.

Connectez-vous pour commenter.

Réponse acceptée

Jan
Jan le 19 Mar 2021
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 commentaires
Jan
Jan le 20 Mar 2021
Modifié(e) : Jan le 22 Mar 2021
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 le 20 Mar 2021
Modifié(e) : hasan s le 21 Mar 2021
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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Performance and Memory dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by