Plotting 4D matrix in 2D space

21 views (last 30 days)
Imad El Ddine Ghandour
Imad El Ddine Ghandour on 10 Sep 2021
Answered: the cyclist on 11 Sep 2021
Hi,
I would like to plot a scatter plot of 4D points in a 2D plane.
This is the line out of my code that I am using:
plot(data(:,1),data(:,2), data(:,3), data(:,4), 'k.');
Whenever I plot the data, some of it plots and some of it ends up with random lines of garabage.
Attached is a screenshot of the results that I am getting by using the above line of code.
How can I get this to be plotted in 2D?
Thanks!

Answers (1)

the cyclist
the cyclist on 11 Sep 2021
You are using the plot command in a way that you wish it worked, without carefully understanding how the syntax actually works. You cannot just put in four columns of data into a command, and hope it does what you want.
Here is some simplified data, and your plotting code:
data = [1 1 4 4;
2 3 5 6;
3 2 6 5];
figure
h = plot(data(:,1),data(:,2), data(:,3), data(:,4),'k.');
set(h,'MarkerSize',16) % Adding this to make the dots more visible
If you carefully read the documentation you will see that your code is plotting two pairs of data against each other: data(:,1) vs. data(:,2), and data(:,3) vs. data(:4). Then you add a 'LineSpec' (black dots) to the second pair of plots, while the first set uses the default, which is a blue line.
This code is functionally equivalent to
data = [1 1 4 4;
2 3 5 6;
3 2 6 5];
figure
hold on
plot(data(:,1),data(:,2)); % First plot, default to blue line connecting points
h = plot(data(:,3), data(:,4),'k.'); % Second plot, using black dots
set(h,'MarkerSize',16)
It's not clear to me what you actually want the plot to be. If you want the projection of the the 4-dimensional data onto 2 dimensions, then I believe you simply ignore the 3rd and 4th dimension, and plot
figure
h = plot(data(:,1),data(:,2),'k.');
set(h,'MarkerSize',16)

Community Treasure Hunt

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

Start Hunting!