MATLAB Answers

Translated by

このページのコンテンツは英語から自動翻訳されています。自動翻訳をオフにする場合は「<a class="turn_off_mt" href="#">ここ</a>」をクリックしてください。

sharadhi
0

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

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 件のコメント

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.

サインイン to comment.

タグ

2 件の回答

回答者: Rik Wisselink
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 件のコメント

I used c(j) instead of c and got what I needed.
Thank you Rik

サインイン to comment.


回答者: KSSV
2019 年 2 月 11 日

YOu can specify a random colour using rand(1,3)

  0 件のコメント

サインイン to comment.



Translated by