Plotting elements of a matrix in one vertical line for every iteration

3 views (last 30 days)
The script loops through a set of commands 1000 times. In every iteration, a sparse matrix with some non-zero values are generated.
What I want to do is:
For one iteration, plot all the non-zero elements of the matrix with specific colours(Y-axis) along a vertical line(=iteration number,X-axis). Repeat the same for every iteration. This way I get to see how one particular element of the matrix is varying through every iteration.
This is what I did:
figure();hold on;
for i=1:1000
%%script to generate Matrix
Matrix;
[row,col,s]=find(Matrix);
for j=1:size(row)
scatter(i,Matrix(row(j),col(j)));
end
end
Problem: Since I am plotting one element at a time, the colours are random and cannot show how a specific element (say (1,3) of Matrix) varies through the iterations.
  1 Comment
Rik
Rik on 11 Feb 2019
You can specify the marker color, so the only thing you'll have to do is come up with a method to convert your index to a specific color.

Sign in to comment.

Accepted Answer

Rik
Rik on 11 Feb 2019
As I mentioned in my comment, you need to find a way to convert a position in your matrix to a color. In my example below I'm using the linear index to land on the same spot of the colormap.
figure();hold on;
CMap=colormap;
for i=1:1000
%%script to generate Matrix
Matrix;
[row,col,s]=find(Matrix);
for j=1:size(row)
c_ind=sub2ind(size(Matrix),row(j),col(j));
c_ind=mod(c_ind,size(CMap,1))+1;
c=CMap(c_ind,:);
scatter(i,Matrix(row(j),col(j)),[],c);
end
end
You could also edit the code to remove the second loop.

More Answers (1)

KSSV
KSSV on 11 Feb 2019
YOu can specify a random colour using rand(1,3)

Community Treasure Hunt

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

Start Hunting!