i am getting error in this code like "Subscripted assignment dimension mismatch". please tell how to solve it

tic
clc;
r=input('enter the cell radius in meters=');
N=input('enter the no of cells=');
S=3;
x(1)=1;
y(1)=1;
p(1,:)=[x(1),y(1)];
x=zeros(N,2);
y=zeros(N,2);
h=zeros(N,2);
P=zeros(N,1);
for i=2:N
x(i)=(i-1)*1.5*r;
h(i)=(i-1)*sqrt(3)*r;
y(i)=h(i)^2-x(i)^2;
p(i,:)=[x(i),y(i)];
end
for i=1:S
k=randint(1,1,[1 N]);
P(i,:)=p(k,:);
end
disp(P)
%finding the received signal strength captured by all sensor nodes
theta=2*pi*rand;
X=r*cos(theta);
Y=r*sin(theta);
M=[X,Y];
sum=0;
d=zeros(1,100);
for i=1:S
for j=1:2
a=sum+(M(1,j)-P(i,j))^2;
end
d(i)=sqrt(a);
end
d0=10;
n=4;
MRSS=-10*n*log(d0);
RSS=zeros(1,100);
t=zeros(100,100);
for i=1:S
RSS(i)=(MRSS-10*n*log(d(i)/d0))/5;
end
%finding stongest rssi and generating mapping circle
for i=1:S
d(i)=d0*10^((MRSS-RSS(i))/(10*n));
end
for i=1:S
for j=1:2
t(i,j)=(d(i))^2+M(1,j);
end
end
[v p]=min(d);
xth =x(p)+d(p)*cos(theta);
yth=y(p)+d(p)*sin(theta);
D(1,:)=[x(p)+d(p)*cos(theta),y(p)+d(p)*sin(theta)];
figure,
plot(t(1:3,1),t(1:3,2),'rs','MarkerEdgeColor','k',...
'MarkerFaceColor','g',...
'MarkerSize',4)
grid on
hold on
xlabel('x co-ordinate of sensor nodes')
ylabel('y co-ordinates of sensor nodes')
legend
plot(D(1,1),D(1,2),'bs','MarkerEdgeColor','k',...
'MarkerFaceColor','r',...
'MarkerSize',4)
%results
fprintf('RSSIs collected from 3 sensor nodes=')
disp(RSS)
fprintf('distance of mobile user from all 3 sensor nodes=')
disp(d)
fprintf('co-ordinates of the new location of the mobile user are=')
disp(D)
toc

1 Comment

You use nonstandard function randint. The function randint is being obsoleted, and should be replaced with randi.

Sign in to comment.

 Accepted Answer

This code has many little "features" that could be simplified, for example:
  • try to avoid using both uppercase and lowercase letters for variable names, e.g. p and P.
  • x and y are defined rather awkwardly using x(1)=1, y(1)=1). Just use x=1 and y=1 instead.
  • many variables are expanded within loops (i.e. without any array preallocation).
  • multiple loops could be replaced with much simpler vectorized code (see below).
In any case the problem is very simple: the line P(i,:)=p(k,:) tries to allocate two columns of p to one column of P, thus the "dimensions mismatch" error. Two columns cannot fit into one column.
The best solution is to completely rewrite the whole code using vectorized code, which will be neater, faster and less buggy anyway:
rad = str2double(input('enter the cell radius in meters=','s'));
num = str2double(input('enter the no of cells=','s'));
S = 3;
V = 0:num-1;
p(:,1) = V(:)*1.5*rad;
p(:,2) = (V(:)*sqrt(3)*rad).^2 - p.^2;
p(1,:) = 1;
P = p(randi(num,1,S),:)
disp(P)

4 Comments

Parvez's "Answer" moved here because it's not an answer to the original question.
but after writing ur code it is showing error like "Undefined function or method 'randi' for input arguments of type 'double'".
What version of MATLAB are you using? What does this say
>> which -all randi
There seems to have been some changes to randi after that version. Try this line instead:
P = p(randint(1,S,num),:)

Sign in to comment.

More Answers (0)

Categories

Find more on Mathematics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!