Creating Binary Plot From Data
Mostrar comentarios más antiguos
I am trying to use conditional statements to create a plot from a binary matrix. I cannot get matlab to create a binary matrix; let alone create the plot. The code I have thus far:
format long
dist;
a = [new_data{1, 1}.subj_xPath.',new_data{1, 1}.subj_yPath.'];
b = [new_data{1, 1}.eye_X.',new_data{1, 1}.eye_Y.'];
dist = sqrt((a(:,1) - b(:,1)).^2 + (a(:,2) - b(:,2)).^2);
nrows = 133;
ncols = 1;
A = ones(nrows,ncols);
for p = 1:ncols
for q = 1:nrows
if dist <= 100
A(q,p) = 1;
else
A(q,p) = 0;
end
end
end
I hope to create an plot that looks similar to

Thank you in advance!
Respuestas (2)
Star Strider
el 24 de Jul. de 2016
0 votos
I’m not certain what you want to do, and I can’t run your code.
4 comentarios
Courtney Thomas, Jr.
el 24 de Jul. de 2016
Star Strider
el 25 de Jul. de 2016
The problem is here:
if dist <= 100
Your ‘dist’ variable is a column vector, and at each step you are testing the entire vector.
What do you want to do?
Courtney Thomas, Jr.
el 25 de Jul. de 2016
Star Strider
el 25 de Jul. de 2016
Editada: Star Strider
el 25 de Jul. de 2016
That I understand.
I have a Neural Network Toolbox called dist, so changing it to ‘dst_v’, you can use the hypot function:
dst_v = hypot((a(:,1)-b(:,2)), (a(:,2)-b(:,2)));
How do you want to index it? As you have written your code, ‘A’ is going to be a vector that replicates ‘dist’:
A = dst_v <= 100;
That will produce a logical vector. To produce a numeric vector:
A = +(dst_v <= 100);
You can use reshape to create a matrix from it, but I don’t know what dimensions you want your matrix to be.
Image Analyst
el 25 de Jul. de 2016
I'm not sure why your desired plot is a 1-D plot but you're creating a 2D array for "A". Why is that? And your desired plot is a tri or quad level plot, not a binary one. Why is that? And what are A and dist? Are they 1-D vectors or 2-D matrices? About all I can figure from your code is this:
format long
dist;
a = [new_data{1, 1}.subj_xPath.',new_data{1, 1}.subj_yPath.'];
b = [new_data{1, 1}.eye_X.',new_data{1, 1}.eye_Y.'];
dist = sqrt((a(:,1) - b(:,1)).^2 + (a(:,2) - b(:,2)).^2);
A = dist <= 100; % Binary or matrix
if ndims(A) == 1
% A is a vector
plot(A, 'b-');
grid on;
elseif ndims(A) == 2
% A is a 2-D array
imshow(A, []);
axis on;
end
2 comentarios
Courtney Thomas, Jr.
el 25 de Jul. de 2016
Image Analyst
el 25 de Jul. de 2016
What is new_data? We can't run your code because you didn't supply it. Is it a table, a cell array, a structure, structures inside cells of a cell array? Whatever, it's kind of complicated to figure out so it's best if you just supply it to us.
With your code, which is equivalent to my code
A = dist <= 100;
You're going to end up with a binary vector with 2 levels, not a tri- or quad-leveled matrix. Why do you think it should? What ranges would you want to use to set the output to the 3 or 4 output levels, like 0-1 gets mapped to 1, 1-4 gets mapped to 2, 4-20 gets mapped to 3, or whatever. You have to say what ranges get mapped to each of the 3 or 4 levels you want.
Categorías
Más información sobre 2-D and 3-D Plots en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!