MATLAB Answers

Convert two vectors (x and y) to a square matrix ?

7 views (last 30 days)
Vingt100
Vingt100 on 24 Mar 2020
Edited: Stephan on 24 Mar 2020
So here is my problem.
I have two vectors that descibe a plot (a sinusiodal function) and I want to have the discretized matrix of this plot. It means that I want to have a 1 on the curve and 0 everywhere else.
Is this possible ?

  5 Comments

Show 2 older comments
Vingt100
Vingt100 on 24 Mar 2020
My x data is x = 0:0.01:pi and my ydata is the function for example y = sin(x) + cos(x)
J. Alex Lee
J. Alex Lee on 24 Mar 2020
do you want a pixel map (image) of the plot?
Vingt100
Vingt100 on 24 Mar 2020
Yes i'd like to transform my plot into an image.

Sign in to comment.

Accepted Answer

Stephan
Stephan on 24 Mar 2020
Edited: Stephan on 24 Mar 2020
x = 0:0.01:6.28;
y = sin(x);
M = ones(size(x,2));
x_int = 1:size(x,2);
y_int = ceil(y * size(y,2)/(abs(min(y))+abs(max(y))) +...
size(y,2)/(abs(min(y))+abs(max(y))));
y_int(y_int>size(M,2)) = y_int(y_int>size(M,2))-1;
for k = 1:size(y,2)
M(y_int(k),x_int(k)) = 0;
end
imshow(M)

  0 Comments

Sign in to comment.

More Answers (0)