From: Shamma Raghib on

Hello all. I have written the code below which is giving me an error.

I want 'LS' to be the summation of- the values of ditances between the MS position and the RS (FROM 1 to 6) whereas I want 'd' to be the distance calculation for one MS to the first RS only (RS(1))

MSX and MSY are the x and y coordinates for MS
RSX and RSY are the x and y coordinates for RS. There are six such points placed.
MS positions are random.
the distance function is simple distance calculation between two points.(not shown here)
Please help :) ....would be grateful.


clc; clear all;
R=500;
user_max=1:1:10;
Fs=4e6;
Nsub=512;
BLER=0.1;
B=Fs/Nsub;
Gap = -log(5*BLER)/1.5;
gamma=1/Gap;
user_r = zeros(user_max, 1); user_theta = zeros(user_max, 1);
for user = user_max
relay_r=[R R R R R R];
relay_theta=[0 60 120 180 240 300];

user_r(user, 1) = rand*R/6;
user_theta(user, 1) = rand * 180;
[MSX,MSY]=pol2cart(user_theta,user_r); %transform MS position to cartesian


d_tot=0;
for j=1:6
[RSX,RSY]=pol2cart(relay_theta(j),relay_r(j)); %transform RS position to the cartesian coordinate system


hold on;
plot(RSX,RSY,'b*');
end
for j=1:6
d=distance(MSX,MSY,RSX(j),RSY(j));
end
d_tot=d_tot+d(j)^-gamma;
LS=distance(MSX,MSY,RSX(1),RSY(1));


%%%%%%%% rest code not relevant to query.


plot(MSX,MSY,'r*');
hold off;

end
display(d);
From: Andy on
Well, since you haven't posted your distance function, the only error I'm getting is that the distance function is undefined. Could you post your error message, rather than forcing us to guess at it?
From: Shamma Raghib on
"Andy " <myfakeemailaddress(a)gmail.com> wrote in message <i3p8gg$g6r$1(a)fred.mathworks.com>...
> Well, since you haven't posted your distance function, the only error I'm getting is that the distance function is undefined. Could you post your error message, rather than forcing us to guess at it?


ERROR:

??? Attempted to access RSX(2); index out of bounds because numel(RSX)=1.
Error in ==> polytry2 at 29
d=distance(MSX,MSY,RSX(j),RSY(j))

Here is my distance function:
Like I said its plain and simple :)

%%%%%%%%
function d=distance(a, b, x, y)
d=sqrt((a-x).^2+(b-y).^2);

end



%%%%%

Hope you can help :) cheers!


From: Jan Simon on
Dear Shamma,

> for j=1:6
> [RSX,RSY]=pol2cart(relay_theta(j),relay_r(j));
> end

This overwrites RSX and RSY in each iteration. Finally these variables are scalar.

> for j=1:6
> d=distance(MSX,MSY,RSX(j),RSY(j));
> end

But now you try to access RSX(2) etc. As the error message tells exactly, RSX is scalar only.

Do you know the debugger? Type:
dbstop if error
stop stop when an error occurs. Or set a break point in your program an step thorugh the code line by line. You can inspect the variables in the command line during debugging.
It is a very powerful tool and can find such problems faster than a newsgroup.

Kind regards, Jan
From: Andy on
The error message is clear. RSX is not what you think it is. When I run your code, I get:

>> RSX
RSX =
-11.0483

You then ask for the second element in RSX (which isn't there), and you get an error.