MATLAB giving error while trying to create a 3D scatter plot: Error using sparse (Index exceeds array bounds)
Show older comments
I was following the MathWorks example: https://www.mathworks.com/help/stats/tsne.html#bvkcm24-1 which works fine for fisheriris dataset.
load fisheriris
[Y2,loss2] = tsne(meas,'Algorithm','exact','NumDimensions',3);
figure
v = double(categorical(species));
c = full(sparse(1:numel(v),v,ones(size(v)),numel(v),3));
scatter3(Y2(:,1),Y2(:,2),Y2(:,3),15,c,'filled')
title('3-D Embedding')
view(-50,8)
However, when I change a dataset to something else, it gives error:
Error using sparse
Index exceeds array bounds.
I have the following:
c = full(sparse(1:numel(v), v, ones(size(v)), numel(v), 3));
where numel(v) = 9339, size(v) = 9339 x 1 double
Here the last argument is digit '3' which I followed from MathWorks example in link above. I want to reduce it to 3 dimensions and create a 3D plot same as in that example.
In my case v has 25 classes unlike 3 in Mathworks exaple. So I changed it as follows:
c = full(sparse(1:numel(v), v, ones(size(v)), numel(v), 25));
This time there is no error in c, but I get the error below:
Error using scatter3 (line 109)
C must be a single color, a vector the same length as X, or an M-by-3 matrix.
10 Comments
Walter Roberson
on 18 Jul 2018
scatter3(Y2(:,1),Y2(:,2),Y2(:,3),15,v,'filled');
colormap( jet(numel(unique(v))) )
Walter Roberson
on 18 Jul 2018
Edited: Walter Roberson
on 18 Jul 2018
I know your case v has 25 classes. That is why I gave the code that I gave. Though I guess you might possibly need
scatter3(Y2(:,1), Y2(:,2), Y2(:,3), 15, v(:), 'filled');
colormap( jet(numel(unique(v))) )
Walter Roberson
on 18 Jul 2018
What is size(Y2) and size(v) ?
Walter Roberson
on 18 Jul 2018
No, that cannot be the error, as your code is not invoking pca: your code is invoking tsne(), and tsne will return an array N x 3 where N is the number of rows in the data you are passing tsne.
hello_world
on 18 Jul 2018
Edited: hello_world
on 18 Jul 2018
hello_world
on 18 Jul 2018
Edited: hello_world
on 18 Jul 2018
Walter Roberson
on 18 Jul 2018
(1) Is there still a way to use 'c'?
No. That example only works for exactly 3 classes. The technique of using sparse like that and passing the resulting c in as the color information, works only for the case of 1 class or 3 classes.
(2) Can I assign specific color to each class
Yes. Where I showed
colormap( jet(numel(unique(v))) )
you would instead create a colormap array with as many rows as you have classes, with each row having exactly the colors you want. Then pass the v (class number) as the color data.
(3) How to make legend such that each color represents class name/label?
Use something like
hold on
for K = 1 : 25
LH(K) = plot(nan, nan, 'Color', YourColorMap(K, :));
LS{K} = sprintf('Class #%d\n', K); %adjust to appropriate legend
end
hold off
and then
legend(LH, LS)
hello_world
on 18 Jul 2018
Edited: hello_world
on 18 Jul 2018
Walter Roberson
on 18 Jul 2018
I would prefer you did not close the question, as it might be of use to other people.
To create a colormap array, just assign to it.
mycmap = [0 0 1;
0 0 .5;
0 0 0;
0 .5 0;
0 .5 .5;
0 .5 1;
0 1 1;
0.5 1 1;
0.5 1 0.5;
0.5 1 0;
0.5 0.5 0;
0.5 0 0 ];
colormap(mycmap)
You might be interested in colormapeditor(), or in https://www.mathworks.com/matlabcentral/fileexchange/28943-color-palette-tables-cpt-for-matlab
Accepted Answer
More Answers (0)
Categories
Find more on Sparse Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!