COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### combine columns

• Email
• Follow

```I have a matrix with several columns with numbers in it
for example
A=[ 1  2  4  3;
6  5  7  6;
9  3  3  2;
8  6  2  1;
6  1  2  7]

I want to add a new column, that combines columns 2 en 3, but as one number
so i get
A=[ 1  2  4  3  2-4;
6  5  7  6  5-7;
9  3  3  2  3-3;
8  6  2  1  6-2;
6  1  2  7  1-2]

how do i do this?

(the reason i want to this, is because i want to know the unique combinations of a couple of columns combined, i can now use unique(A(:,5)))
```
 0

See related articles to this posting

```On 10/15/2010 10:24 AM, Mirel wrote:
> I have a matrix with several columns with numbers in it
> for example
> A=[ 1 2 4 3;
> 6 5 7 6;
> 9 3 3 2;
> 8 6 2 1;
> 6 1 2 7]
>
> I want to add a new column, that combines columns 2 en 3, but as one number
> so i get A=[ 1 2 4 3 2-4;
> 6 5 7 6 5-7;
> 9 3 3 2 3-3;
> 8 6 2 1 6-2;
> 6 1 2 7 1-2]
>
> how do i do this?
>
> (the reason i want to this, is because i want to know the unique
> combinations of a couple of columns combined, i can now use unique(A(:,5)))
the simplest I can think, efficiency is arguable...
A = [A A(:,2)-A(:,3)]
```
 0

```Umut Tabak <umut.tabak@gmail.com> wrote in message <6e538\$4cb81062\$82a112b1\$24497@news1.tudelft.nl>...
> On 10/15/2010 10:24 AM, Mirel wrote:
> > I have a matrix with several columns with numbers in it
> > for example
> > A=[ 1 2 4 3;
> > 6 5 7 6;
> > 9 3 3 2;
> > 8 6 2 1;
> > 6 1 2 7]
> >
> > I want to add a new column, that combines columns 2 en 3, but as one number
> > so i get A=[ 1 2 4 3 2-4;
> > 6 5 7 6 5-7;
> > 9 3 3 2 3-3;
> > 8 6 2 1 6-2;
> > 6 1 2 7 1-2]
> >
> > how do i do this?
> >
> > (the reason i want to this, is because i want to know the unique
> > combinations of a couple of columns combined, i can now use unique(A(:,5)))
> the simplest I can think, efficiency is arguable...
> A = [A A(:,2)-A(:,3)]

That doesn't work
in the last column, i know get the values of column 2 minus the values of column 3
but i want it as one number with a line in between
```
 0

```>> A = [A A(:,2)-A(:,3)]
>
> That doesn't work

did u try?

> in the last column, i know get the values of column 2 minus the values
> of column 3

so what is the above code doing?

> but i want it as one number with a line in between

I don't understand anything from this sentence...

```
 0

```Umut Tabak <umut.tabak@gmail.com> wrote in message <5b2ee\$4cb818ae\$82a112b1\$24601@news1.tudelft.nl>...
>

i want a number in the form 45-89-3
but maby matlab doesn't see that as a number bus as a string
in that case i just want to ad a string, that consists out of a couple of columns
```
 0

```On 10/15/2010 11:32 AM, Mirel wrote:
> Umut Tabak <umut.tabak@gmail.com> wrote in message
> <5b2ee\$4cb818ae\$82a112b1\$24601@news1.tudelft.nl>...
>>
>
> i want a number in the form 45-89-3
> but maby matlab doesn't see that as a number bus as a string
> in that case i just want to ad a string, that consists out of a couple
> of columns
then you should use a different data structure such as a cell array to
form the last column as a string for your needs.
```
 0

```Dear Mirel,

> i want a number in the form 45-89-3

This is not a number.
Please take the time and think again: What exactly do you want to get?

Jan
```
 0

```
"Mirel " <crazy_girl_531@hotmail.com> wrote in message
news:i993nq\$c1r\$1@fred.mathworks.com...
> Umut Tabak <umut.tabak@gmail.com> wrote in message
> <6e538\$4cb81062\$82a112b1\$24497@news1.tudelft.nl>...
>> On 10/15/2010 10:24 AM, Mirel wrote:
>> > I have a matrix with several columns with numbers in it
>> > for example
>> > A=[ 1 2 4 3;
>> > 6 5 7 6;
>> > 9 3 3 2;
>> > 8 6 2 1;
>> > 6 1 2 7]
>> >
>> > I want to add a new column, that combines columns 2 en 3, but as one
>> > number
>> > so i get A=[ 1 2 4 3 2-4;
>> > 6 5 7 6 5-7;
>> > 9 3 3 2 3-3;
>> > 8 6 2 1 6-2;
>> > 6 1 2 7 1-2]
>> >
>> > how do i do this?
>> >
>> > (the reason i want to this, is because i want to know the unique
>> > combinations of a couple of columns combined, i can now use
>> > unique(A(:,5)))
>> the simplest I can think, efficiency is arguable...
>> A = [A A(:,2)-A(:,3)]
>
> That doesn't work
> in the last column, i know get the values of column 2 minus the values of
> column 3
> but i want it as one number with a line in between

That is not possible, since "one number with a line in between" is not a
number but a _string_.  You _could_ construct the string using NUM2STR and
concatenation, but you couldn't then store that string inside the _numeric_
array.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
http://www.mathworks.com

```
 0
Reply slord (13686) 10/15/2010 1:46:26 PM

```too bad it's not possible,
```
 0