If I have two matrixes of same dimensions, and I type:
plot(A,B)
The code automatically associates each row of matrix A to the same row of matrix B by realizing a different number of curves corresponding to the row number of the matrixes?

 Akzeptierte Antwort

dpb
dpb am 20 Nov. 2021

0 Stimmen

Description
plot(X,Y) creates a 2-D line plot of the data in Y versus the corresponding values in X.
  • If X and Y are both vectors, then they must have equal length. The plot function plots Y versus X.
  • If X and Y are both matrices, then they must have equal size. The plot function plots columns of Y versus columns of X.

7 Kommentare

Emilio Pulli
Emilio Pulli am 20 Nov. 2021
Bearbeitet: Emilio Pulli am 20 Nov. 2021
Therefore, if I want to plot each row of matrix Y versus each row of matrix X, I cannot use the directly plot(X,Y), can I? I think I should use a for loop…
dpb
dpb am 20 Nov. 2021
Bearbeitet: dpb am 20 Nov. 2021
Then just transpose X,Y
plot(X.',Y.')
and now you have them by column as plot() expects.
Many routines (like mean, sum for examples) have an optional dim argument that allows you to control the dimension over which the function operates, but plot() was written to allow multiple sets of X,Y,linespec triplets in earliest incarnations and trying to add in the dimension over which to operate was just too much baggage. So, you have to arrange the data as expected...
Emilio Pulli
Emilio Pulli am 20 Nov. 2021
Bearbeitet: Emilio Pulli am 20 Nov. 2021
I think that I am going to do a for loop with the counter guiding the row by writing:
figure hold on for j=1:size(X,1) plot(X(j,:),Y(j,:)); end hold off
dpb
dpb am 20 Nov. 2021
Bearbeitet: dpb am 20 Nov. 2021
Why? Use MATLAB as it is designed to be used with array operations. That's why it is named MATrix LABoratory.
There is absolutely nothing to be gained by using the loop; only more code to write/maintain and extra overhead.
Also, you'll note you won't get the expected result from the above code by itself.
X=repmat(1:10,5,1); Y=randn(size(X)); % some dummy data by row in arrays
subplot(2,1,1); plot(X.',Y.') % plot arrays transposed
title('Plot Transposed X, Y')
subplot(2,1,2) % now plot in loop by row
for i=1:size(X,1),plot(X(i,:),Y(i,:));end
title('Plot X, Y by Row in Loop')
ylim([-4 2])
results in
What went wrong in the bottom plot?
Emilio Pulli
Emilio Pulli am 20 Nov. 2021
Bearbeitet: Emilio Pulli am 20 Nov. 2021
I am using my code and I am getting the result I want to obtain…moreover, in this way, I can also implement in the plot function the function “DisplayName” to create a loop legend. I know that also your suggestion is good but it does not fit properly with my case
Emilio Pulli
Emilio Pulli am 20 Nov. 2021
Obviously I use also the hold on and hold on function…
dpb
dpb am 20 Nov. 2021
Bearbeitet: dpb am 20 Nov. 2021
That's also simple enough without the loop...
hL=plot(X.',Y.');
set(hL,{'DisplayName'},compose('Line %02d',1:size(X,1)).')
legend(hL)
Salt the string generation to suit...any set of parameters that can be codified in line can be used in lookup array fashion or computed dynamically as well.
Or, just use legend() directly to achieve same result --
legend(compose('Line %02d',1:size(X,1)).','Location','northwest')

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Interpolation of 2-D Selections in 3-D Grids finden Sie in Hilfe-Center und File Exchange

Tags

Gefragt:

am 20 Nov. 2021

Bearbeitet:

dpb
am 20 Nov. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by