How can I increase the size of an array that is already in the workspace with increments of 0.1?

2 Ansichten (letzte 30 Tage)
I am doing my dissertation for my masters and I ran into a problem, I am trying to compute the error between a smooth spline and the original line, however the arrays do not match in size, I am tryingg to increase the original array which has a size of 45x2, how can I create a new array from this smaller array with increment increase of 0.1
Thank you
  12 Kommentare
Mathieu NOE
Mathieu NOE am 9 Jul. 2021
yet another issue - now I lack one toolbox to continue
'distance' requires one of the following:
Antenna Toolbox
Automated Driving Toolbox
Communications Toolbox
Computer Vision Toolbox
Mapping Toolbox
Navigation Toolbox
Phased Array System Toolbox
Sensor Fusion and Tracking Toolbox
Error in Filipe>explore_successors (line 225)
successors(successor_count,3) = h+distance(x_cell,y_cell,s_x,s_y);%h
Error in Filipe (line 114)
successors=explore_successors(cell_x,cell_y,path_cost,x_target,y_target,in_valid,max_x,max_y);
Mathieu NOE
Mathieu NOE am 9 Jul. 2021
easily solved by creating my own function - that wasn't too much complicated ....
%%%%%%%%%%%%%%%%%
function d = mydistance(x1,y1,x2,y2)
d = sqrt((x1-x2).^2 + (y1-y2).^2);
end

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Mathieu NOE
Mathieu NOE am 9 Jul. 2021
so this is the status now :
1/ I believe I fixed a couple of issues at the end of the main code :
now both linear and spline curves have same dimensions and error can be computed :
% m=path(:,1);
% m1=x_start;
% m2=m1:0.1:m;
x1=[path(:,1);x_start]; % seemed to me the start point is missing in "path" so added manually at the bottom
y1=[path(:,2);y_start]; % seemed to me the start point is missing in "path" so added manually at the bottom
xq=(min(x1):0.1:max(x1))'; % correction here also
vq =interp1(x1,y1,xq,'spline'); %see interp1() for other interpolation methods. Extrapolation is dangerous.
y2 =interp1(x1,y1,xq,'linear'); %see interp1() for other interpolation methods. Extrapolation is dangerous.
plot(xq,y2,xq,vq,'b','LineWidth',2);
error_rms = sqrt(mean((y2-vq).^2))
error_rms = 0.0227
PLOT :
Full code :
clc
clearvars
max_x=50;
max_y=30;
environment=2*(ones(max_x,max_y));
j=0;
x_val = 1;
y_val = 1;
axis([1 max_x+1 1 max_y+1])
grid on;
grid minor
hold on;
n=0;
xval=48;
yval=28;
x_target=xval;
y_target=yval;
environment(xval,yval)=0;%Initialize environment with location of the target
plot(xval,yval,'gd');
text(xval+1,yval+.5,'Target')
% obstacle 1
for i = 9:14
for j = 24:27
environment(i,j) = -1;
%plot(i+.5,j+.5,'rd');
end
end
x = [9.5, 14.5, 14.5, 9.5];
y = [24.5, 24.5, 27.5, 27.5];
fill(x,y,'k');
% obstacle 2
for i = 17:23
for j = 21:25
environment(i,j) = -1;
%plot(i+.5,j+.5,'rd');
end
end
x = [17.5, 23.5, 23.5, 17.5];
y = [21.5, 21.5, 25.5, 25.5];
fill(x,y,'k');
% obstacle 3
for i = 27:33
for j = 20:27
environment(i,j) = -1;
%plot(i+.5,j+.5,'rd');
end
end
x = [27.5, 33.5, 33.5, 27.5];
y = [20.5, 20.5, 27.5, 27.5];
fill(x,y,'k');
% obstacle 4
for i = 28:33
for j = 11:18
environment(i,j) = -1;
%plot(i+.5,j+.5,'rd');
end
end
x = [28.5, 33.5, 33.5, 28.5];
y = [11.5, 11.5, 18.5, 18.5];
fill(x,y,'k');
% obstacle 5
for i = 37:41
for j = 11:27
environment(i,j) = -1;
%plot(i+.5,j+.5,'rd');
end
end
x = [37.5, 41.5, 41.5, 37.5];
y = [11.5, 11.5, 27.5, 27.5];
fill(x,y,'k');
%plot(19+.5,19+.5,'gd');
xval=3;
yval=20;
x_start=xval;%Starting Position
y_start=yval;%Starting Position
environment(xval,yval)=1;
plot(xval,yval,'bo');
valid=[]; % x | y | parent_x | parent_y | h(n) | g(n) | f(n)
in_valid=[]; % x | y
k=1;
for i=1:max_x
for j=1:max_y
if(environment(i,j) == -1) % check if obstacle
in_valid(k,1)=i;
in_valid(k,2)=j;
k=k+1;
end
end
end
in_valid_size=size(in_valid,1);
cell_x=x_start;
cell_y=y_start;
valid_size=1; % initialize size of valid_list
path_cost=0;
goal_distance=sqrt((cell_x-x_target)^2 + (cell_y-y_target)^2);
new_row=[1,8];
new_row(1,1)=1;
new_row(1,2)=cell_x;
new_row(1,3)=cell_y;
new_row(1,4)=cell_x; % parent x
new_row(1,5)=cell_y; % parent y
new_row(1,6)=path_cost;
new_row(1,7)=goal_distance;
new_row(1,8)=goal_distance;
valid(valid_size,:)=new_row; % initializing path with start position
valid(valid_size,1)=0;
in_valid_size=in_valid_size+1;
in_valid(in_valid_size,1)=cell_x; % make it invalid for further iterations
in_valid(in_valid_size,2)=cell_y;
path_not_found=1;
while((cell_x ~= x_target || cell_y ~= y_target) && path_not_found == 1)
% x | y | h | g | f
successors=explore_successors(cell_x,cell_y,path_cost,x_target,y_target,in_valid,max_x,max_y);
successors_size=size(successors,1);
for i=1:successors_size
flag=0;
for j=1:valid_size
if(successors(i,1) == valid(j,2) && successors(i,2) == valid(j,3) ) % if successor same as already existing cell inpath
% disp('valid')
% valid
% disp(' ');
valid(j,8)=min(valid(j,8),successors(i,5)); % check for minimum f and then pick it
if valid(j,8) == successors(i,5)
valid(j,4)=cell_x;% parent x
valid(j,5)=cell_y;% parent y
valid(j,6)=successors(i,3); % h
valid(j,7)=successors(i,4); % g
end;
flag=1;
end;
end;
if flag == 0 % if new cell with minimum f(n) then add to valid_path
valid_size= valid_size+1;
new_row=[1,8];
new_row(1,1)=1;
new_row(1,2)=successors(i,1);
new_row(1,3)=successors(i,2);
new_row(1,4)=cell_x; % parent x
new_row(1,5)=cell_y; % parent y
new_row(1,6)=successors(i,3); % h
new_row(1,7)=successors(i,4); % g
new_row(1,8)=successors(i,5); % f
valid(valid_size,:)= new_row;
end;
end;
index_min_cell = min_f(valid,valid_size,x_target,y_target);
if (index_min_cell ~= -1) % if index with minimum fn is obstacle no path exists
cell_x=valid(index_min_cell,2);
cell_y=valid(index_min_cell,3);
path_cost=valid(index_min_cell,6);
in_valid_size=in_valid_size+1; % put the cell in_valid so we dont come back on it again
in_valid(in_valid_size,1)=cell_x;
in_valid(in_valid_size,2)=cell_y;
valid(index_min_cell,1)=0;
else
path_not_found=0;
end;
end;
% backtracking to find the path
i=size(in_valid,1);
path=[];
xval=in_valid(i,1); % pick last in in_valid_list that must be target
yval=in_valid(i,2);
i=1;
path(i,1)=xval;
path(i,2)=yval;
i=i+1;
if ( (xval == x_target) && (yval == y_target))
inode=0;
parent_x=valid(find((valid(:,2) == xval) & (valid(:,3) == yval),1),4);
parent_y=valid(find((valid(:,2) == xval) & (valid(:,3) == yval),1),5);
while( parent_x ~= x_start || parent_y ~= y_start)
path(i,1) = parent_x;
path(i,2) = parent_y;
inode=find((valid(:,2) == parent_x) & (valid(:,3) == parent_y),1);
parent_x=valid(inode,4);
parent_y=valid(inode,5);
i=i+1;
end;
% plottin path
j=size(path,1);
p=plot(path(j,1),path(j,2),'bo');
j=j-1;
for i=j:-1:1
pause(.25);
set(p,'XData',path(i,1),'YData',path(i,2));
drawnow ;
end;
plot(path(:,1),path(:,2));
else
disp( 'Sorry, No path exists to the Target!');
end
% m=path(:,1);
% m1=x_start;
% m2=m1:0.1:m;
x1=[path(:,1);x_start]; % seemed to me the start point is missing in "path" so added manually at the bottom
y1=[path(:,2);y_start]; % seemed to me the start point is missing in "path" so added manually at the bottom
xq=(min(x1):0.1:max(x1))'; % correction here also
vq =interp1(x1,y1,xq,'spline'); %see interp1() for other interpolation methods. Extrapolation is dangerous.
y2 =interp1(x1,y1,xq,'linear'); %see interp1() for other interpolation methods. Extrapolation is dangerous.
plot(xq,y2,xq,vq,'b','LineWidth',2);
error_rms = sqrt(mean((y2-vq).^2))
%%%%%%%%%%%%%%%%%
function d = mydistance(x1,y1,x2,y2)
d = sqrt((x1-x2).^2 + (y1-y2).^2);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function successors=explore_successors(x_cell,y_cell,h,x_target,y_target,in_valid,max_x,max_y)
successors=[];
successor_count=1;
c2=size(in_valid,1);
for k= 1:-1:-1
for j= 1:-1:-1
if (k~=j || k~=0) %The node itself is not its successor
s_x = x_cell+k;
s_y = y_cell+j;
if( (s_x >0 && s_x <=max_x) && (s_y >0 && s_y <=max_y)) % successor within the matrix
flag=1;
for c1=1:c2
if(s_x == in_valid(c1,1) && s_y == in_valid(c1,2)) % successor not an obstacle or already visited
flag=0;
end;
end;
if (flag == 1)
successors(successor_count,1) = s_x;
successors(successor_count,2) = s_y;
% successors(successor_count,3) = h+distance(x_cell,y_cell,s_x,s_y);%h
successors(successor_count,3) = h+mydistance(x_cell,y_cell,s_x,s_y);%h
% successors(successor_count,4) = distance(x_target,y_target,s_x,s_y);%g
successors(successor_count,4) = mydistance(x_target,y_target,s_x,s_y);%g
successors(successor_count,5) = successors(successor_count,3)+successors(successor_count,4);%f
successor_count=successor_count+1;
end
end
end
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function index_of_min = min_f(valid,valid_len,x_target,y_target)
temp_array=[];
k=1;
flag=0;
goal_index=0;
for j=1:valid_len
if (valid(j,1)==1)
temp_array(k,:)=[valid(j,:) j];
if (valid(j,2)==x_target && valid(j,3)==y_target)
flag=1;
goal_index=j;
end;
k=k+1;
end;
end;
if flag == 1
index_of_min=goal_index;
end
if size(temp_array ~= 0)
[min_f,temp_min]=min(temp_array(:,8));
%min_f
index_of_min=temp_array(temp_min,9);
fprintf('Cell with minimum f found to be x : %d y : %d with f = %f',valid(index_of_min,2),valid(index_of_min,3),valid(index_of_min,8));
else
index_of_min=-1;
end;
end
  3 Kommentare
Filipe Silva
Filipe Silva am 9 Jul. 2021
It is perfect, thank you very much!
I also have to ask
function d = mydistance(x1,y1,x2,y2)
d = sqrt((x1-x2).^2 + (y1-y2).^2);
end
Its not the first time I see the function above that you have included, could I ask what it does?
Thank you
Mathieu NOE
Mathieu NOE am 12 Jul. 2021
hello
this compute the euclidian distance between two points . Simply I had to write this function becasue in your code you are using a similar function from a toolbox I don't have. But as you can see that was easily solved.
bye

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

KSSV
KSSV am 9 Jul. 2021
You need to use interp1.
Let A be your original array of size 45*2.
dx = 0.1 ;
x = A(:,1) ; y = A(:,2) ;
xi = min(x):dx:max(x) ;
yi = interp1(x,y,xi) ;
  1 Kommentar
Filipe Silva
Filipe Silva am 9 Jul. 2021
Hello, Can you check my last comment above? maybe might give an understanding of what I need, I already made the interpolation, but now I need to compute the error between original and interpolated, the code is there to help
Thank you

Melden Sie sich an, um zu kommentieren.


Mathieu NOE
Mathieu NOE am 9 Jul. 2021
so , maybe now I acn provide an answer....
had a couple of questions to myself - and tried to solve it this way :
now we have both the linear and spline path data the same size
y2 is basically the same linear y1 data but smapled at the new delta = 0.1 (using interp1 again but with "linear" argument;
below is the end of the main code that I changed quite a bit :
% m=path(:,1);
% m1=x_start;
% m2=m1:0.1:m;
x1=[path(:,1);x_start]; % seemed to me the start point is missing in "path" so added manually at the bottom
y1=[path(:,2);y_start]; % seemed to me the start point is missing in "path" so added manually at the bottom
xq=(min(x1):0.1:max(x1))'; % correction here also
vq =interp1(x1,y1,xq,'spline'); %see interp1() for other interpolation methods. Extrapolation is dangerous.
y2 =interp1(x1,y1,xq,'linear'); %see interp1() for other interpolation methods. Extrapolation is dangerous.
plot(xq,y2,xq,vq,'b','LineWidth',2);
error_rms = sqrt(mean((y2-vq).^2));
%%%%%%%%%%%%%%%%%
function d = mydistance(x1,y1,x2,y2)
d = sqrt((x1-x2).^2 + (y1-y2).^2);
end
result is : error_rms = 0.0227
plot :
entire code below :
clc
clearvars
max_x=50;
max_y=30;
environment=2*(ones(max_x,max_y));
j=0;
x_val = 1;
y_val = 1;
axis([1 max_x+1 1 max_y+1])
grid on;
grid minor
hold on;
n=0;
xval=48;
yval=28;
x_target=xval;
y_target=yval;
environment(xval,yval)=0;%Initialize environment with location of the target
plot(xval,yval,'gd');
text(xval+1,yval+.5,'Target')
% obstacle 1
for i = 9:14
for j = 24:27
environment(i,j) = -1;
%plot(i+.5,j+.5,'rd');
end
end
x = [9.5, 14.5, 14.5, 9.5];
y = [24.5, 24.5, 27.5, 27.5];
fill(x,y,'k');
% obstacle 2
for i = 17:23
for j = 21:25
environment(i,j) = -1;
%plot(i+.5,j+.5,'rd');
end
end
x = [17.5, 23.5, 23.5, 17.5];
y = [21.5, 21.5, 25.5, 25.5];
fill(x,y,'k');
% obstacle 3
for i = 27:33
for j = 20:27
environment(i,j) = -1;
%plot(i+.5,j+.5,'rd');
end
end
x = [27.5, 33.5, 33.5, 27.5];
y = [20.5, 20.5, 27.5, 27.5];
fill(x,y,'k');
% obstacle 4
for i = 28:33
for j = 11:18
environment(i,j) = -1;
%plot(i+.5,j+.5,'rd');
end
end
x = [28.5, 33.5, 33.5, 28.5];
y = [11.5, 11.5, 18.5, 18.5];
fill(x,y,'k');
% obstacle 5
for i = 37:41
for j = 11:27
environment(i,j) = -1;
%plot(i+.5,j+.5,'rd');
end
end
x = [37.5, 41.5, 41.5, 37.5];
y = [11.5, 11.5, 27.5, 27.5];
fill(x,y,'k');
%plot(19+.5,19+.5,'gd');
xval=3;
yval=20;
x_start=xval;%Starting Position
y_start=yval;%Starting Position
environment(xval,yval)=1;
plot(xval,yval,'bo');
valid=[]; % x | y | parent_x | parent_y | h(n) | g(n) | f(n)
in_valid=[]; % x | y
k=1;
for i=1:max_x
for j=1:max_y
if(environment(i,j) == -1) % check if obstacle
in_valid(k,1)=i;
in_valid(k,2)=j;
k=k+1;
end
end
end
in_valid_size=size(in_valid,1);
cell_x=x_start;
cell_y=y_start;
valid_size=1; % initialize size of valid_list
path_cost=0;
goal_distance=sqrt((cell_x-x_target)^2 + (cell_y-y_target)^2);
new_row=[1,8];
new_row(1,1)=1;
new_row(1,2)=cell_x;
new_row(1,3)=cell_y;
new_row(1,4)=cell_x; % parent x
new_row(1,5)=cell_y; % parent y
new_row(1,6)=path_cost;
new_row(1,7)=goal_distance;
new_row(1,8)=goal_distance;
valid(valid_size,:)=new_row; % initializing path with start position
valid(valid_size,1)=0;
in_valid_size=in_valid_size+1;
in_valid(in_valid_size,1)=cell_x; % make it invalid for further iterations
in_valid(in_valid_size,2)=cell_y;
path_not_found=1;
while((cell_x ~= x_target || cell_y ~= y_target) && path_not_found == 1)
% x | y | h | g | f
successors=explore_successors(cell_x,cell_y,path_cost,x_target,y_target,in_valid,max_x,max_y);
successors_size=size(successors,1);
for i=1:successors_size
flag=0;
for j=1:valid_size
if(successors(i,1) == valid(j,2) && successors(i,2) == valid(j,3) ) % if successor same as already existing cell inpath
% disp('valid')
% valid
% disp(' ');
valid(j,8)=min(valid(j,8),successors(i,5)); % check for minimum f and then pick it
if valid(j,8) == successors(i,5)
valid(j,4)=cell_x;% parent x
valid(j,5)=cell_y;% parent y
valid(j,6)=successors(i,3); % h
valid(j,7)=successors(i,4); % g
end;
flag=1;
end;
end;
if flag == 0 % if new cell with minimum f(n) then add to valid_path
valid_size= valid_size+1;
new_row=[1,8];
new_row(1,1)=1;
new_row(1,2)=successors(i,1);
new_row(1,3)=successors(i,2);
new_row(1,4)=cell_x; % parent x
new_row(1,5)=cell_y; % parent y
new_row(1,6)=successors(i,3); % h
new_row(1,7)=successors(i,4); % g
new_row(1,8)=successors(i,5); % f
valid(valid_size,:)= new_row;
end;
end;
index_min_cell = min_f(valid,valid_size,x_target,y_target);
if (index_min_cell ~= -1) % if index with minimum fn is obstacle no path exists
cell_x=valid(index_min_cell,2);
cell_y=valid(index_min_cell,3);
path_cost=valid(index_min_cell,6);
in_valid_size=in_valid_size+1; % put the cell in_valid so we dont come back on it again
in_valid(in_valid_size,1)=cell_x;
in_valid(in_valid_size,2)=cell_y;
valid(index_min_cell,1)=0;
else
path_not_found=0;
end;
end;
% backtracking to find the path
i=size(in_valid,1);
path=[];
xval=in_valid(i,1); % pick last in in_valid_list that must be target
yval=in_valid(i,2);
i=1;
path(i,1)=xval;
path(i,2)=yval;
i=i+1;
if ( (xval == x_target) && (yval == y_target))
inode=0;
parent_x=valid(find((valid(:,2) == xval) & (valid(:,3) == yval),1),4);
parent_y=valid(find((valid(:,2) == xval) & (valid(:,3) == yval),1),5);
while( parent_x ~= x_start || parent_y ~= y_start)
path(i,1) = parent_x;
path(i,2) = parent_y;
inode=find((valid(:,2) == parent_x) & (valid(:,3) == parent_y),1);
parent_x=valid(inode,4);
parent_y=valid(inode,5);
i=i+1;
end;
% plottin path
j=size(path,1);
p=plot(path(j,1),path(j,2),'bo');
j=j-1;
for i=j:-1:1
pause(.25);
set(p,'XData',path(i,1),'YData',path(i,2));
drawnow ;
end;
plot(path(:,1),path(:,2));
else
disp( 'Sorry, No path exists to the Target!');
end
% m=path(:,1);
% m1=x_start;
% m2=m1:0.1:m;
x1=[path(:,1);x_start]; % seemed to me the start point is missing in "path" so added manually at the bottom
y1=[path(:,2);y_start]; % seemed to me the start point is missing in "path" so added manually at the bottom
xq=(min(x1):0.1:max(x1))'; % correction here also
vq =interp1(x1,y1,xq,'spline'); %see interp1() for other interpolation methods. Extrapolation is dangerous.
y2 =interp1(x1,y1,xq,'linear'); %see interp1() for other interpolation methods. Extrapolation is dangerous.
plot(xq,y2,xq,vq,'b','LineWidth',2);
error_rms = sqrt(mean((y2-vq).^2));
%%%%%%%%%%%%%%%%%
function d = mydistance(x1,y1,x2,y2)
d = sqrt((x1-x2).^2 + (y1-y2).^2);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function successors=explore_successors(x_cell,y_cell,h,x_target,y_target,in_valid,max_x,max_y)
successors=[];
successor_count=1;
c2=size(in_valid,1);
for k= 1:-1:-1
for j= 1:-1:-1
if (k~=j || k~=0) %The node itself is not its successor
s_x = x_cell+k;
s_y = y_cell+j;
if( (s_x >0 && s_x <=max_x) && (s_y >0 && s_y <=max_y)) % successor within the matrix
flag=1;
for c1=1:c2
if(s_x == in_valid(c1,1) && s_y == in_valid(c1,2)) % successor not an obstacle or already visited
flag=0;
end;
end;
if (flag == 1)
successors(successor_count,1) = s_x;
successors(successor_count,2) = s_y;
% successors(successor_count,3) = h+distance(x_cell,y_cell,s_x,s_y);%h
successors(successor_count,3) = h+mydistance(x_cell,y_cell,s_x,s_y);%h
% successors(successor_count,4) = distance(x_target,y_target,s_x,s_y);%g
successors(successor_count,4) = mydistance(x_target,y_target,s_x,s_y);%g
successors(successor_count,5) = successors(successor_count,3)+successors(successor_count,4);%f
successor_count=successor_count+1;
end
end
end
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function index_of_min = min_f(valid,valid_len,x_target,y_target)
temp_array=[];
k=1;
flag=0;
goal_index=0;
for j=1:valid_len
if (valid(j,1)==1)
temp_array(k,:)=[valid(j,:) j];
if (valid(j,2)==x_target && valid(j,3)==y_target)
flag=1;
goal_index=j;
end;
k=k+1;
end;
end;
if flag == 1
index_of_min=goal_index;
end
if size(temp_array ~= 0)
[min_f,temp_min]=min(temp_array(:,8));
%min_f
index_of_min=temp_array(temp_min,9);
fprintf('Cell with minimum f found to be x : %d y : %d with f = %f',valid(index_of_min,2),valid(index_of_min,3),valid(index_of_min,8));
else
index_of_min=-1;
end;
end

Kategorien

Mehr zu Sparse Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by