Pergunta

Is there a way to pass an argument multiple times to different arrays?

What I want to do is:

r = '1:10:end'; % This doesn't work for me
plot(x1(r), y1(r));
plot(x2(r), y2(r));
...

and pass r to different arrays (with different lengths) in many plot functions. I tried with [r] but no success.

Foi útil?

Solução

As I understand it, you want to plot every 10th element of possibly different sized arrays. There are a few ways you could do this. One way would be to write a short function to filter your arrays for you, for instance:

plot_10 = @(x,y) plot(x(1:10:end),y(1:10:end));
plot_10(x1,y1);
plot_10(x2,y2);
...

EDIT: Just an additional thought. If you wanted to enable the extended functionality of plot (e.g. passing line/colour arguments, etc). You could do something like this:

plot_10 = @(x,y,varargin) plot(x(1:10:end),y(1:10:end),varargin{:});
plot_10(x1,t1,'k+');

Outras dicas

To use the "end" operator, it needs to be inside an array access call;

n = 10;
r = 1 : 1 : n;
r(1:end) % is legal
r(1:floor(end/2)) % is legal

So you could do something like this:

s = rand(1,2*n);
s(r)

% to compare...
s( 1:1:n )
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top