In a function F(x,:), what does the colon represent?

5 views (last 30 days)
I am new to Matlab.
I am trying to understand the following piece of code.
for g=1:GA.Ng
x(g,:)=GA.xmn(g)+(GA.xmx(g)-GA.xmn(g))*P(g,:);
end
what does the : indicate in x(g,:), P(g,:)? Thanks

Answers (2)

Star Strider
Star Strider on 21 Feb 2016
‘... what does the : indicate in x(g,:), P(g,:)? ...’
In 2D arrays, it is necessary to address the row in the first element of the subscript reference, and the columns in the second. In both of the references you mention, the row is specified as whatever value ‘g’ is, and the isolated colon ‘:’ is shorthand indicating all the columns in the row designated by the value of ‘g’.

Jan
Jan on 21 Feb 2016
Edited: Jan on 21 Feb 2016
x(g,:) is an abbreviation for x(g, 1:end).
If X is an array with more than 2 dimensions, X(k, :) contains the values of the sub-array:
X = reshape([1,2,3,4, 11,12,13,14, 21,22,23,24], [2, 2, 3])
X(1, :)
X(2, :)
Please note, that these are Matlab basics and you can learn them efiiciently reading the "Getting Started" chapters of the documentation.

Community Treasure Hunt

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

Start Hunting!