X = [X1 X2]; - Please Explain

17 views (last 30 days)
Ananth
Ananth on 12 Jan 2013
Commented: Walter Roberson on 18 Apr 2023
I am beginner of Matlab, Please explain the following line. X = [X1 X2];
  1 Comment
Jan
Jan on 12 Jan 2013
The forum is not the right place to explain the basics. The Getting Started chapters of the documentation explain them exhaustively and therefore it is strongly recommended to read them carefully.

Sign in to comment.

Answers (3)

Azzi Abdelmalek
Azzi Abdelmalek on 12 Jan 2013
Edited: Azzi Abdelmalek on 12 Jan 2013
X1=12
X2=24
X = [X1 X2] % create a line vector X by horizontal concatenation;
Y=[X1;X2] %vertical concatenation
You have to read this

Walter Roberson
Walter Roberson on 12 Jan 2013

Hari
Hari on 17 Apr 2023
x=[x 1]
  1 Comment
Walter Roberson
Walter Roberson on 18 Apr 2023
... more than you wanted to know ;-)
  • In the case where x is an empty numeric or char array (not string or logical) of no more than 2 dimensions, then the value 1 would be converted to the same class as the input x and the resulting 1 would be assigned to become the new value of x. conversion to char is by applying char() which gives the corresponding UNICODE character positions -- for example char(1) is UNICODE U+0001 and not '1'
  • In the case where x is an empty logical array of no more than 2 dimensions, then x would be assigned double precision 1 (the 1 would not be converted to logical)
  • In the case where x is the 1 x 0 empty string array (no other size of empty), then the output would be "1" -- string() applied to the constant 1. Here the "1" is the character for the digit 1, U+0031, not by looking up as a binary code position, not U+0001 . conversion to string formats numeric into text; conversion to char() does not
  • In the case where x is an empty string array that is not 1 x 0, then the operation is an error
  • in the case where x is an empty numeric array that is not double precision, or is a char array, or logical, or string, and the array has more than 2 dimensions, then the operation is an error -- for example, [zeros(256,512,11,0,19,'single'), 1] is an error
  • In the case where x is an empty double precision array of any dimension, then x would be assigned double precision 1 -- for example, [zeros(256,512,11,0,19), 1] is valid -- but only if x is double precision
  • In the case where x is a numeric or char (not logical or string) row vector (exactly one row, any number of columns, no non-trivial hyperdimensions), then the value 1 would be converted to the same class as x and added to the end of the row and the output would be the input x with the converted 1 appended to the single row. conversion to char is by applying char() which gives the corresponding UNICODE character positions -- for example char(1) is UNICODE U+0001 and not '1'
  • In the case where x is a logical vector (exactly one row, any number of columns, no non-trivial hyperdimensions), then x will be converted to a double precision row, and numeric 1 will be appened to that row, and the output will be a double precision row
  • In the case where x is a string() vector (exactly one row, any number of colums, no non-trivial hyperdimensions), then the 1 will have string() applied to it, producing "1", and that "1" will be appended to the end of the row vector as a new element. ["abc" 1] will result in ["abc" "1"], not in "abc1" -- see the string + operator to merge strings. Here the "1" is the character for the digit 1, U+0031, not by looking up as a binary code position, not U+0001 . conversion to string formats numeric into text; conversion to char() does not
  • In the case where x is not numeric or char or logical or strign, the results are defined by the class methods; the results are well-defined for some classes, but is an error for other classes

Sign in to comment.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!