Reputation: 9823
I'm just learning matlab and I have a snippet of code which I don't understand the syntax of. The x is an n x 1 vector.
Code is below
p = (min(x):(max(x)/300):max(x))';
The p vector is used a few lines later to plot the function
plot(p,pp*model,'r');
Upvotes: 0
Views: 65
Reputation: 1
This code examines an array named x
, and finds its minimum value min(x)
and its maximum value max(x)
. It takes the maximum value and divides it by the constant 300.
It doesn't explicitly name any variable, setting it equal to max(x)/300
, but for the sake of explanation, I'm naming it "incr", short for increment.
And, it creates a vector named p
. p
looks something like this:
p = [min(x), min(x) + incr, min(x) + 2*incr, ..., min(x) + 299*incr, max(x)];
Upvotes: 0
Reputation: 8459
Certainly looks to me like an odd thing to be doing. Basically, it's creating a vector of values p
that range from the smallest to the largest values of x
, which is fine, but it's using steps between successive values of max(x)/300
.
If min(x)=300
and max(x)=300.5
then this would only give 1 point for p
.
On the other hand, if min(x)=-1000
and max(x)=0.3
then p
would have thousands of elements.
In fact, it's even worse. If max(x)
is negative, then you would get an error as p
would start from min(x)
, some negative number below max(x)
, and then each element would be smaller than the last.
I think p
must be used to create pp
or model
somehow as well so that the plot works, and without knowing how I can't suggest how to fix this, but I can't think of a good reason why it would be done like this. using linspace(min(x),max(x),300)
or setting the step to (max(x)-min(x))/299
would make more sense to me.
Upvotes: 1
Reputation: 6162
It generates an arithmetic progression.
An arithmetic progression is a sequence of numbers where the next number is equal to the previous number plus a constant. In an arithmetic progression, this constant must stay the same value.
In your code,
min(x)
is the initial value of the sequencemax(x) / 300
is the increment amountmax(x)
is the stopping criteria. When the result of incrementation exceeds this stopping criteria, no more items are generated for the sequence.I cannot comment on this particular choice of initial value and increment amount, without seeing the surrounding code where it was used.
However, from a naive perspective, MATLAB has a linspace
command which does something similar, but not exactly the same.
Upvotes: 1