Plotting n-by-m matrix

10 ビュー (過去 30 日間)
Matthias Wurm
Matthias Wurm 2019 年 1 月 18 日
編集済み: madhan ravi 2019 年 1 月 18 日
Hello,
a simple question: I've n-by-m matrices for X and Y. When I plot them, I get m differently colored lines connecting n points.
Example:
plot([1,1.1;2,2.1;3,3.1],[3,3.5;4,4.5;6,5],'-o')
With n=3 and m=2,
In the degenreated case with n=1, I expect and need to get m differently colored points. Instead the m points are connected to a single line.
Compare:
plot([1,1.1],[3,3.5],'-o')
How can I prevent this in this degenerated case?
Best regards
  4 件のコメント
Stephen23
Stephen23 2019 年 1 月 18 日
編集済み: Stephen23 2019 年 1 月 18 日
@madhan ravi: I like your fresh ideas, and you often bring me to think about how solutions to problems are approached, which actually I find really useful to consider not just how but why some solutions work (or can be better solved in other ways).
With something like MATLAB there just as many special cases as there are consistent rules, and those special cases most of the time are useful, but occasionally trip the user up because they expect something consistent with some other pattern... such as in this question.
Your idea to translate the inputs was certainly intuitive, but when both inputs are vectors then they will be interpreted as one line, regardless of their orientation (see the plot help). Vectors are a special case! That is why I use NaN to force plot to interpret each column as its own "line" of one point. There certainly might be other solutions.
madhan ravi
madhan ravi 2019 年 1 月 18 日
編集済み: madhan ravi 2019 年 1 月 18 日
@Stephen Cobeldick: Thank you very much :) now everything is clear as a crystal (really so happy about it), to be frank and honest in the short time I was able to improve the skills is because of you. I like the way you interpret with solutions deeply and it really tells how much of an experienced man you are , keep inspiring!

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

採用された回答

Stephen23
Stephen23 2019 年 1 月 18 日
編集済み: Stephen23 2019 年 1 月 18 日
Good question. I also faced this issue a few years ago, and I found the simplest solution was to append NaN onto the bottom of the matrices, e.g.:
X(end+1,:) = NaN;
Y(end+1,:) = NaN;
plot(X,Y,'-o')
This forces MATLAB to treat each column as its own "line", even if it contains only one data point, while the NaNs are of course not plotted (so you do not need a special case, the NaNs can be appended to matrices of any size). Here is an example with your data:
>> X = [1,1.1];
>> Y = [3,3.5];
>> X(end+1,:) = NaN;
>> Y(end+1,:) = NaN;
>> plot(X,Y,'-o')
  1 件のコメント
Matthias Wurm
Matthias Wurm 2019 年 1 月 18 日
Thank you Stephen!
This trick works!

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

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by