# Why does it not plot the values

4 views (last 30 days)
sparsh garg on 1 Sep 2021
Commented: Walter Roberson on 1 Sep 2021
So let's say you have smoe X and Y.
let's say you have a set of indices in another array called flag
now when i do this
plot(X(flag),Y(flag)
,a nice plot is displayed
but when I do
A=[X(flag),Y(flag)];
and then do
plot(A(1,:),A(2,:))
the plot doesn't show.
Any ideas why this is happening?

Walter Roberson on 1 Sep 2021
When X is a row vector and Y is a row vector, and flag is a vector, then regardless of whether flag is a row vector or a column vector, the output of X(flag) will be a row vector and the output of Y(flag) would be a row vector. When you did [X(flag, Y(flag)] then that would be concatenating together two row vectors horizontally, and the result would be a row vector. Your plot attempt A(2,:) would then be trying to use row 2 of a row vector, which would not exist, and you would get an error
When X is a column vector and Y is a column vector, and flag is a vector, then regardless of whether flag is a row vector or a column vector, the output of X(flag) will be a column vector and the output of Y(flag) would be a column vector. When you did [X(flag, Y(flag)] then that would be concatenating together two column vectors horizontally, and the result would be a something-by-2 matrix. Your plot attempt A(1,:), A(2,:) would then be extracting row 1 of the matrix with 2 columns, to plot against row 2 of the matrix with 2 columns. That would draw a single line segment from (x(1),x(2)) to (y(1),y(2)) instead of the (x(1),y(1)) to (x(2),y(2)) that you would have expected.
You did not mention any error message, so that tends to suggest that your X and Y are both column vectors. In that case, what you should be doing is
plot(A(:,1),A(:,2))
sparsh garg on 1 Sep 2021
tried doing that now gives me indice exceeds matrix dimension
X_pts=[edges2.x,edges.y];
X_pts = unique(X_pts,'rows');
x=X_pts(:,1);
y = X_pts(:,2);
FP1=[x(marker);y(marker)];
figure(1);
plot(x(marker),y(marker))
set(gca,'YDir','reverse')
figure(2);
plot(FP1(:,1),FP1(:,2));
Walter Roberson on 1 Sep 2021
You said in your question that you were doing
A=[X(flag),Y(flag)];
but in your actual code we see
FP1=[x(marker);y(marker)];
The difference between that comma compared to the semi-colon is important !!
I will have a look later after I get some sleep.