How can this Python code be written in Matlab? I'm trying to write a Matlab script that will allow two Stepper motors to scan in a Raster pattern but all I can find is this Python script. How the 'for' loop be replicated in Matlab?
14 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
David Sweeney
el 15 de Nov. de 2017
Comentada: Rik
el 17 de Nov. de 2017
from pylab import *
# define some grids
xgrid = arange(20, 31)
ygrid = arange(10, 16)
xscan = []
yscan = []
for i, yi in enumerate(ygrid):
xscan.append(xgrid[::(-1)**i]) # reverse when i is odd
yscan.append(ones_like(xgrid) * yi)
# squeeze lists together to vectors
xscan = concatenate(xscan)
yscan = concatenate(yscan)
# quick plot
plot(xscan, yscan, '.-')
axis([19, 31, 9, 16])
show()
4 comentarios
Rik
el 16 de Nov. de 2017
That is not the Matlab part of your problem. What is it you want to do? It looks like you want to plot some dotted line path. Is that the case? If so, what constraints are there?
Respuesta aceptada
Rik
el 17 de Nov. de 2017
Editada: Rik
el 17 de Nov. de 2017
There is a Cody question that asked you to produce a matrix like the one below
X=[1 2 3;
6 5 4;
7 8 9];
My solution was a variation of the code below
x_num_steps=round((xmax-xmin)/xstep)+1;
y_num_steps=round((xmax-xmin)/xstep)+1;
sort_order=reshape(1:(x_num_steps*y_num_steps),x_num_steps,y_num_steps)';
sort_order(2:2:end,1:end)=sort_order(2:2:end,end:-1:1);
You can use this matrix to sort your coordinate grid (which you can generate with meshgrid). (You will need to transpose it to get the correct order.) [X,Y]=meshgrid(linspace(xmin,xmax,x_num_steps),linspace(ymin,ymax,y_num_steps)); %vectorize and sort X and Y sort_order=sort_order(:); X=X(:);Y=Y(:); X=X(sort_order);Y=Y(sort_order);
So, here all of the code:
%initialize variables
ymin=0;
ystep=1;
ymax=1;
xmin=0;
xstep=1;
xmax=5;
%calculate the number of steps in each direction (i.e. grid lines)
%this also catches the case where the gap isn't an integer multiple of the step size
x_num_steps=round((xmax-xmin)/xstep)+1;
y_num_steps=round((xmax-xmin)/xstep)+1;
%create a matrix with 1:total number of steps (grid points)
sort_order=reshape(1:(x_num_steps*y_num_steps),x_num_steps,y_num_steps)';
sort_order(2:2:end,1:end)=sort_order(2:2:end,end:-1:1);
sort_order=sort_order';
%sort_order contains the order in which we should go through the X and Y matrices
[X,Y]=meshgrid(linspace(xmin,xmax,x_num_steps),linspace(ymin,ymax,y_num_steps));
%vectorize and sort X and Y
sort_order=sort_order(:);
X=X(:);Y=Y(:);
X=X(sort_order);Y=Y(sort_order);
%plot the line and zoom out a bit to show the path
plot(X,Y,'--')
axis([xmin-xstep xmax+xstep ymin-ystep ymax+ystep])
3 comentarios
Más respuestas (2)
Guillaume
el 17 de Nov. de 2017
What I'm trying to do in this code is start at some point within the grid say (0,0) then move a fixed amount of steps in the x direction until say (5,0) then take a step in the y direction to (5,1) then step backwards in the x direction to say (0,1).
What is the difficulty with that? And why use a loop for that (let alone two)?
xmin = 0; xmax = 5; xstep = 1;
ymin = 0; ymax = 1; ystep = 1;
%step 1: go from xmin to xmax, in xstep. y stays at ymin:
x = xmin:xstep:xmax;
y = repelem(xmin, numel(x));
xy = [x; y];
%step 2: go from ymin to max, in ystep. x stays where it is:
y = ymin+ystep:ystep:ystep;
x = repelem(xy(1, end), numel(y));
xy = [xy, [x; y]];
%step 3: go back to xmin. y stays constant
x = xmax-xstep:-xstep:xmin;
y = repelem(xy(2, end), numel(x));
xy = [xy, [x; y]]
3 comentarios
Rik
el 17 de Nov. de 2017
You shouldn't try to do it with a loop. If you want to, you can still print either my solution or the solution by Guillaume, but why would you?
If you want a plot, you just use plot(X,Y,'--') with my solution, or a similar call with this solution.
Andrei Bobrov
el 17 de Nov. de 2017
Editada: Andrei Bobrov
el 17 de Nov. de 2017
[ii,jj] = ndgrid(20:30,10:15);
ii(:,2:2:end) = flip(ii(:,2:2:end));
plot(ii(:),jj(:));
axis([19, 31, 9, 16]);
0 comentarios
Ver también
Categorías
Más información sobre Call Python from MATLAB en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!