Reputation: 374
I am trying to understand the following code I found online and am having trouble with the first line:
errors(:,:) = (errors(:,:)*trials + 0.5)./(trials + 1);
PBIerr = (errors(:,2)-errors(:,3))./(errors(:,2)+errors(:,3));
What does (:,:) stand for in Matlab? Is it some kind of "select all"?
Thanks in advance!
Upvotes: 0
Views: 144
Reputation: 35525
Yes. The colon operator is a "select all, from this index" operator. Note that in the first line, its unnecesary as long as errors
was already 2D, as "select the entire variable" is not something you need to specify, its by default. So it could have been
errors = (errors*trials + 0.5)./(trials + 1);
.
If errors
had more dimensions than 2, then the colon operator in the right hand side of the equal sign is doing something, in particular "A(:,:) reshapes all elements of A into a two-dimensional matrix. This has no effect if A is already a matrix or vector.". The one in the left hand side is useless anyway, as that line overwrites the variable.
The use of the colon operator in the second line however is well justified.
There are few other things that the colon operator means in MATLAB, read them all here: https://uk.mathworks.com/help/matlab/ref/colon.html
Upvotes: 5