Why does the line plot depend on the generator type in a for loop ?

7 Ansichten (letzte 30 Tage)
I have :
  • a set of points in two dimensions given by their coordinates x_stations and y_stations;
  • a set of couples of points given by the indices of point 1 (couples_st1) and point 2 (couples_st2) for each couple;
  • a subset i_subset of those couples
I would like to plot i_subset graphically as lines linking point 1 and point 2 for each couple in the subset.
The problem : depending how I am looping on i_subset :
for i = i_subset
i_couple = i;
% some code...
% generate plot
% ouptut the plotted vectors
end
or
for i = 1:length(i_subset)
i_couple = i_subset(i);
% some code ...
% generate plot
% ouptut the plotted vectors
end
I get the same output of plotted vectors, but not the same plots. Why ?
A minimal example is enclosed with input data. Just run "main_minimal_example.m".
main_minimal_example
max difference in x outputs : 0 max difference in y outputs : 0
Thank you !

Akzeptierte Antwort

Voss
Voss am 8 Mär. 2023
The i_subset passed in to make_line_plot.m is a column vector, and that is assigned to generator if mode == 1. Then generator is used in the for loop
for i_gen = generator
A for loop iterates over the columns of what you give it, so this loop iterates one time, with i_gen being generator.
When mode is 0, then generator is 1:length(i_subset), which is a row vector, and the for loop operates as intended (I assume).
To get the mode == 1 case to work like the mode == 0 case, one way would be to make the for loop iterate over elements of generator, as in:
for i_gen = generator(:).'
[ From the documentation for for, when you have for index = valArray:
valArray — Create a column vector, index, from subsequent columns of array valArray on each iteration. For example, on the first iteration, index = valArray(:,1). The loop executes a maximum of n times, where n is the number of columns of valArray ]
  2 Kommentare
Alexandre Kazantsev
Alexandre Kazantsev am 15 Mär. 2023
Bearbeitet: Alexandre Kazantsev am 15 Mär. 2023
Great answer, i've just understood something basic. Thank you

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Descriptive Statistics and Visualization finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by