Borrar filtros
Borrar filtros

How add another loop in program

1 visualización (últimos 30 días)
hasan s
hasan s el 18 de Mzo. de 2021
Editada: Jan el 22 de Mzo. de 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 comentarios
hasan s
hasan s el 18 de Mzo. de 2021
Editada: hasan s el 18 de Mzo. de 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 el 19 de Mzo. de 2021
I do not exactly understand, what you want to change.

Iniciar sesión para comentar.

Respuesta aceptada

Jan
Jan el 19 de Mzo. de 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 comentarios
Jan
Jan el 20 de Mzo. de 2021
Editada: Jan el 22 de Mzo. de 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 el 20 de Mzo. de 2021
Editada: hasan s el 21 de Mzo. de 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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by