Creating Binary Plot From Data

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!

Risposte (2)

Star Strider
Star Strider il 24 Lug 2016

0 voti

I’m not certain what you want to do, and I can’t run your code.
For the plot, see if the spy function will do what you want.

4 Commenti

I updated the code for you to try running again.
1. From a data sets 'a' and 'b', which are xy coordinates in a 133x2, I calculated distance. I am now trying to use conditional statements to tell matlab "Create a binary matrix. For each distance ≤ 100 input a 1 and if greater a 0.
2. Once a binary matrix is created I would like to plot it to look like the figure above. If a 1, a bar is plotted; if 0, it is left blank.
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?
I would like for matlab to give me a 1 or 0 for each item in the "dist" vector. For each distance ≤ 100 input a 1 and if greater a 0.
Star Strider
Star Strider il 25 Lug 2016
Modificato: Star Strider il 25 Lug 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.

Accedi per commentare.

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 Commenti

A should be a 1-D array. Thanks for pointing that out. My desired plot is a tri or quad level plot because I need to create binary matrices for more than one data sets. I hope to plot each on one graph.
A is the binary matrix produced. dist are the 1x1 calculated results from the
sqrt((a(:,1) - b(:,1)).^2 + (a(:,2) - b(:,2)).^2)
%The distance between point in 'a' and point in 'b'.
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.

Accedi per commentare.

Categorie

Scopri di più su 2-D and 3-D Plots in Centro assistenza e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by