0

As the title says, I want to covert a line(or any equation) to 2D matrix.

For example: If I have a line equation y = x, then I want it to be like:

0 0 0 0 0 1
0 0 0 0 1 0
0 0 0 1 0 0
0 0 1 0 0 0
0 1 0 0 0 0
1 0 0 0 0 0

and the length of rows and columns can be variable.

Is there a function or method to implement that?

Yonggoo Noh
  • 1,811
  • 3
  • 22
  • 37

3 Answers3

2

use meshgrid to get x-y grids:

% set resolution parameters
xmin = 1;
xmax = 100;
dx = 0.1;
ymin = 1;
ymax = 100;
dy = 0.1;
% set x-y grid
[xg, yg] = meshgrid(xmin:dx:xmax, ymin:dy:ymax);
% define your function
f = @(x) (x - 30).^2;
% apply it on the x grid and get close y values
D = abs(f(xg) - yg);
bw = D <= 1;
figure;
imshow(bw);
% flip y axis
set(gca,'YDir','normal')

you get: enter image description here

and then you can further dilate/erode/skeletonize the output

user2999345
  • 4,195
  • 1
  • 13
  • 20
2

Given x and y coordinates, how to fill in a matrix with those coordinates? As it is said, just do it in a for loop, or use the "sub2ind" function.

% x,y coordinates 
x=0:.01:30;
y=10*sin(2*pi*.1*x);

% add offset so that (x,y)-coordinates are always  positive
x=x+abs(min(x))+1;
y=y+abs(min(y))+1;

figure,plot(x,y,'.');axis tight

x=ceil(x); y=ceil(y);    
im=zeros(max(y),max(x));
ind=sub2ind(size(im),y,x);
im(ind)=1;

figure,imagesc(im),axis image, axis xy;colormap gray;axis tight
xlabel('x'); ylabel('y')

enter image description here

enter image description here

Ozcan
  • 726
  • 4
  • 13
1

Why not doing it on your own? You loop through all x-coordinates of the matrix that you (probably scaled) use as the x for your function and get an y out, that you (probably scaled) can round and then use as a y coordinate for your matrix to set the 1.

yar
  • 1,855
  • 13
  • 26