smilingbuddha
smilingbuddha

Reputation: 14660

Force MATLAB to make only commas as column separators

The other day I discovered the following bug in a couple of places in my MATLAB code

I wanted to enter the column vector in my MATLAB script

[a-b,
 c-d
 e-f]

where a,b,c,d,e,f are long expressions in some variables.

and I entered it in as

 [ a -b ;
   c -d ;
   e -f]

Now MATLAB interprets the second matrix as a 3x2 matrix instead of a column vector. Is there a way/command/function to force MATLAB to use only the comma and NOT any white space characters as a column separator for matrices ?

Upvotes: 1

Views: 128

Answers (3)

Dennis Jaheruddin
Dennis Jaheruddin

Reputation: 21563

Assuming you have a piece of code in which you only want to have column vectors and no matrix, there is a fairly quick solution:

replace {space}+ by +

replace {space}- by -

It is quite safe to do and unless you have complicated expressions in your vector it should do the trick.

Upvotes: 0

Dan
Dan

Reputation: 45752

Well your second matrix does look like it's intended as a 3x2. However, if you do it like this it will be a column vector again:

[a - b;
 c - d;
 e - f]

which to me is a reasonable intuitive distinction between a minus b and a, negative b.

You can also use brackets as Ilya suggested.

Upvotes: 1

Ilya Kobelevskiy
Ilya Kobelevskiy

Reputation: 5345

I don't think there is any way to force matlab to not treat white space this way, since it is interpretive language, and doing so may affect some built-in functions/third-party code. However, you can use parentheses to group data - i.e. (a -b) will still be a single element of the matrix.

Upvotes: 3

Related Questions