From: Ahmed on
I've this code for euclidean distance . The matrix ttt= (815350,1) for RGB Values.

-----------------------------------------------
summation = [];
for j = 0:815350;
summation = sqr(sum( (ttt(t) - ttt(1,1))^ 2) );
end
--------------------------------------------------
when I run this code, matlab is stuck.

What is wrong with that.

when I change the for loop like 10000 it works.
From: Walter Roberson on
Ahmed wrote:
> I've this code for euclidean distance . The matrix ttt= (815350,1)
> for RGB Values.
> -----------------------------------------------
> summation = [];
> for j = 0:815350;
> summation = sqr(sum( (ttt(t) - ttt(1,1))^ 2) );
> end
> --------------------------------------------------
> when I run this code, matlab is stuck.

Why are you throwing away the result of the calculation? The next j
along is going to overwrite summation.

For that matter, why are you looping over j but never use j as an index?
Each iteration of the loop is going to calculate exactly the same value.

I also notice that your first reference to ttt in the line uses one
index, but the second reference uses two. If you are using absolute
indexing (a single dimensional index to a multidimensional array) then
the subtraction will yield a single numeric result, which will then be
squared, and the sum() will be operating on that single numeric result,
making the sum redundant. The sqr() call should probably be sqrt(), but
that would just cancel out the ^2, having the same effect as

summation = abs(ttt(t) - ttt(1,1));

Perhaps you wanted to code something like ttt(t,:) - t(1,1) which would
yield a vector of elements, and sum() over a vector of elements makes
sense. But if you do that, you are going to need to use .^2 instead of
^2 in your code.

There are, of course, more efficient ways to write the code, but the
exact method is going to depend on what you are really trying to do.
From: Bruce on
"Ahmed " <noorsun10(a)gmail.com> wrote in message <hjv54n$515$1(a)fred.mathworks.com>...
> I've this code for euclidean distance . The matrix ttt= (815350,1) for RGB Values.
>
> -----------------------------------------------
> summation = [];
> for j = 0:815350;
> summation = sqr(sum( (ttt(t) - ttt(1,1))^ 2) );
> end
> --------------------------------------------------
> when I run this code, matlab is stuck.
>
> What is wrong with that.
>
> when I change the for loop like 10000 it works.

Hello Ahmed,

I rewrote your code a bit as you had a few mistakes...
you used 't' as an index yet the loop variable is 'j'. Also
you had 'sqr' rather than 'sqrt'. Of course if you have a 'sqr' function then no problem.
Your loop started from 0 but needs
to start from 1. All vectors 9row or column) are indexed from one on...

I created random RGB values to parallel
the data you might use. Of course each run of the script
will produce a different scalar result. Feel free to
replace ttt in my script with your own data and run it again.
I saved my script to euclidean.m and ran it from the editor but of course
you could turn it into a m-file function and return summation as the result.

------------------------------------------------------------------------------------------------
%create random RGB values in a n x 1 vector i.e. 101 = Red + Blue
%you can replace this with your own 'ttt'
n=815350;
ttt= 100*mod(floor(10*rand(n,1)),2) + ...
10*mod(floor(10*rand(n,1)),2) + ...
mod(floor(10*rand(n,1)),2);
%start loop from 1 not zero as this was an invalid index you used
%in your code.
for j = 1:n;
%The index is j not 't' as you had in your code i.e. ttt(t) is wrong.
%sqrt is the correct function name not sqr as you had in
%your code.
summation = sqrt(sum( (ttt(j) - ttt(1,1) )^ 2) ); %scalar result
end
------------------------------------------------------------------------------------------------
Cheers

Bruce
 | 
Pages: 1
Prev: UITAB flickers in Linux
Next: Using fplot(?)