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

1 次查看(过去 30 天)
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 !

采纳的回答

Voss
Voss 2023-3-8
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 ]

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graphics Performance 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by