Help me about looping in matlab

hello all,,
please help me to solving my problems,,
i've some problem in matlab's code especially in looping
my example like this ,,
i've two examples of data
First | Second
8 | 9
7 | 8
New data : (6 6)
and then i wanna insert that data into a formula and that data would be like this
x = (First Data - New Data(1))² + (Second Data - New Data(2))²
Example :
x1 = (8 - 6)² + (9 - 6)²;
x2 = (7 - 6)² + (8 - 6)²;
but i'm so confused about looping in matlab.
I don't know how to convert that formula into matlab's code.
Can someone help me please?
this is my code
%----------I N P U T----------------------
a= input ('Input Number of Data :');
for c=1:a
int2 = ['Input The First Score - ',num2str(c),' :'];
int3 = ['Input The Second Score - ',num2str(c),' :'];
b{c,1} = input(int2);
b{c,2} = input(int3);
end;
d = cell2mat(b);
for i=1:c
for j=1:2
x(i,j)=??????????????
end
end
disp(x)
Please help me :'(

 Respuesta aceptada

Matt J
Matt J el 19 de En. de 2013

0 votos

No need to use cells for b, and you should pre-allocate it as well:
b=zeros(a,2); %pre-allocate!!
for c=1:a
int2 = ['Input The First Score - ',num2str(c),' :'];
int3 = ['Input The Second Score - ',num2str(c),' :'];
b(c,1) = input(int2);
b(c,2) = input(int3);
end;
There's also no need to use a loop to compute x. You can do it in one line as follows
x = ( b(:,1) - NewData(1)).^2 + (b(:,2) - NewData(2)).^2;

4 comentarios

baby
baby el 19 de En. de 2013
hello Matt J,,
Thx for ur help :)
but i still have one question
do u know how to give rank on my data
i mean after i input the score and then calculate it and then the total of the lowest score will get first rank
example
First | Second | Total | Rank
8 | 9 | 13 | 2
7 | 8 | 5 | 1
do u know it?
NB : Sorry for my bad english :D
Matt J
Matt J el 19 de En. de 2013
[~,r]=sort(First+Second,'descend');
Rank(r)=1:a;
baby
baby el 19 de En. de 2013
sorry,,
i don't understand with ur code above
can u explain me what's mean "First+Second" and 'Descend'?
Matt J
Matt J el 19 de En. de 2013
Explain "the total of the lowest score"

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Parallel Computing en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 19 de En. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by