Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations KootK on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Code seems to do the same thing over again

Status
Not open for further replies.

James07

Computer
Nov 5, 2005
1
Hi to all, i am basically a newbie in MATLAB and i have written some code that i would think should go through a matrix and write the values i want in.
W = zeros(150);

% Give values to the 150 values of M so that we have
% 150 (x,y) to make the dataset
M = rand(2,150) * 10;

% Create the matrix
for z = 1:149

for v = 1:149

y_total = (M(2,v+1) - M(2,v))^2;
x_total = (M(1,v+1) - M(1,v))^2;
W(z,v) = sqrt((y_total + x_total)\s^2);
end
end
************************************************
Here, my idea was that i have 150 opints on a graph and all the points are held in M(a column for each x,y point). Then i would want to find the distance between each two points by doing the Pythagoras theorem in W(z,v) and store the data in the appropriate rows and columns in W. My final result is that i get the same number written 150 times in each row of the same column. What am i doing wrong? Thanks for the help :)
 
Replies continue below

Recommended for you

Well if I were you I'd write a very stern letter to Mathworks and let them know that the program is doing exactly what you asked it to, instead of what you wanted.


Try

for z = 1:150

for v = 1:150
y_total = (M(2,z) - M(2,v))^2;
x_total = (M(1,z) - M(1,v))^2;
W(z,v) = sqrt((y_total + x_total)\s^2);

end
end




Cheers

Greg Locock

Please see FAQ731-376 for tips on how to make the best use of Eng-Tips.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor