MATLAB giving error while trying to create a 3D scatter plot: Error using sparse (Index exceeds array bounds)

1 次查看(过去 30 天)
I was following the MathWorks example: which works fine for fisheriris dataset.
load fisheriris
[Y2,loss2] = tsne(meas,'Algorithm','exact','NumDimensions',3);
v = double(categorical(species));
c = full(sparse(1:numel(v),v,ones(size(v)),numel(v),3));
title('3-D Embedding')
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 个评论
hello_world 2018-7-18
编辑:hello_world 2018-7-18
@Walter Roberson:
Understand everything, but how to create colormap array to be used in question (2) & (3) above?
I am closing. If you put your reply as an answer instead of a comment, I can accept it.
Walter Roberson
Walter Roberson 2018-7-18
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 ];



Walter Roberson
Walter Roberson 2018-7-18
Copying from comments:
scatter3(Y2(:,1), Y2(:,2), Y2(:,3), 15, v(:), 'filled');
colormap( jet(numel(unique(v))) )

更多回答(0 个)


Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by