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

3 ビュー (過去 30 日間)
sharadhi
sharadhi 2019 年 2 月 11 日
編集済み: sharadhi 2019 年 2 月 11 日
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 件のコメント
Rik
Rik 2019 年 2 月 11 日
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.

サインインしてコメントする。

採用された回答

Rik
Rik 2019 年 2 月 11 日
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.
  1 件のコメント
sharadhi
sharadhi 2019 年 2 月 11 日
編集済み: sharadhi 2019 年 2 月 11 日
I used c(j) instead of c and got what I needed.
Thank you Rik

サインインしてコメントする。

その他の回答 (1 件)

KSSV
KSSV 2019 年 2 月 11 日
YOu can specify a random colour using rand(1,3)

カテゴリ

Help Center および File ExchangeScatter Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by