Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Transform matrix to vector

Subject: Transform matrix to vector

From: Sprinceana

Date: 6 Jul, 2009 06:08:01

Message: 1 of 22

I have the matrix:

A=[1 2 3;4 5 6; 6 7 8]

And I want to obtain the vector line:

1 2 3 4 5 6 7 8

What I did:

>>A = [1 2 3;4 5 6; 6 7 8] % matrix definition
>>b=A(1,:) % extract line 1 of matrix A in vector b
>>c=A(2,:) % extract line 2 of matrix A in vector c
>>d=A(3,:) % % extract line 3 of matrix A in vector d
>>result=[b c d] %concatenate the 3 vectors b,c,d in a single vector result
>>unique(result) % eliminate doubled elements from vector result

Is there another method to obtain that? I wait your advices!

Subject: Transform matrix to vector

From: nor ki

Date: 6 Jul, 2009 07:10:03

Message: 2 of 22

"Sprinceana " <mihaispr@yahoo.com> wrote in message <h2s4c1$3aj$1@fred.mathworks.com>...
> I have the matrix:
>
> A=[1 2 3;4 5 6; 6 7 8]
>
> And I want to obtain the vector line:
>
> 1 2 3 4 5 6 7 8
>
> What I did:
>
> >>A = [1 2 3;4 5 6; 6 7 8] % matrix definition
> >>b=A(1,:) % extract line 1 of matrix A in vector b
> >>c=A(2,:) % extract line 2 of matrix A in vector c
> >>d=A(3,:) % % extract line 3 of matrix A in vector d
> >>result=[b c d] %concatenate the 3 vectors b,c,d in a single vector result
> >>unique(result) % eliminate doubled elements from vector result
>
> Is there another method to obtain that? I wait your advices!

try:

B = A(:)';

hth
kinor

Subject: Transform matrix to vector

From: Dave Robinson

Date: 6 Jul, 2009 09:42:02

Message: 3 of 22

"Sprinceana " <mihaispr@yahoo.com> wrote in message <h2s4c1$3aj$1@fred.mathworks.com>...
> I have the matrix:
>
> A=[1 2 3;4 5 6; 6 7 8]
>
> And I want to obtain the vector line:
>
> 1 2 3 4 5 6 7 8
>
> What I did:
>
> >>A = [1 2 3;4 5 6; 6 7 8] % matrix definition
> >>b=A(1,:) % extract line 1 of matrix A in vector b
> >>c=A(2,:) % extract line 2 of matrix A in vector c

> >>d=A(3,:) % % extract line 3 of matrix A in vector d
> >>result=[b c d] %concatenate the 3 vectors b,c,d in a single vector result
> >>unique(result) % eliminate doubled elements from vector result
>
> Is there another method to obtain that? I wait your advices!

"Help reshape" maybe

Regards

Dave Robinson

Subject: Transform matrix to vector

From: Nathan

Date: 6 Jul, 2009 16:12:12

Message: 4 of 22

A = unique(reshape(A,1,[]))

That should do it. Good luck.
-Nathan

Subject: Transform matrix to vector

From: Alan B

Date: 6 Jul, 2009 16:24:02

Message: 5 of 22

Nathan <ngreco32@gmail.com> wrote in message <209741dd-abb8-4b85-b3c0-f0f2de55d830@z4g2000prh.googlegroups.com>...
> A = unique(reshape(A,1,[]))
>
> That should do it. Good luck.
> -Nathan

how about unique(A)' ?

Subject: Transform matrix to vector

From: Ali

Date: 22 Apr, 2011 22:07:05

Message: 6 of 22

"Sprinceana " <mihaispr@yahoo.com> wrote in message <h2s4c1$3aj$1@fred.mathworks.com>...
> I have the matrix:
>
> A=[1 2 3;4 5 6; 6 7 8]
>
> And I want to obtain the vector line:
>
> 1 2 3 4 5 6 7 8
>
> What I did:
>
> >>A = [1 2 3;4 5 6; 6 7 8] % matrix definition
> >>b=A(1,:) % extract line 1 of matrix A in vector b
> >>c=A(2,:) % extract line 2 of matrix A in vector c
> >>d=A(3,:) % % extract line 3 of matrix A in vector d
> >>result=[b c d] %concatenate the 3 vectors b,c,d in a single vector result
> >>unique(result) % eliminate doubled elements from vector result
>
> Is there another method to obtain that? I wait your advices!


B=reshape(A',1,9) does it for you!

Subject: Transform matrix to vector

From: dpb

Date: 22 Apr, 2011 22:19:52

Message: 7 of 22

On 4/22/2011 5:07 PM, Ali wrote:
> "Sprinceana " <mihaispr@yahoo.com> wrote in message
> <h2s4c1$3aj$1@fred.mathworks.com>...
>> I have the matrix:
>>
>> A=[1 2 3;4 5 6; 6 7 8]
>>
>> And I want to obtain the vector line:
>>
>> 1 2 3 4 5 6 7 8
...

>> Is there another method to obtain that? I wait your advices!
>
>
> B=reshape(A',1,9) does it for you!

Indeed; too bad that Matlab doesn't allow

v=A'(:)';

to eliminate the explicit reshape() call...closest one can come is

v=A';v=v(:)'

--

Subject: Transform matrix to vector

From: Steven_Lord

Date: 25 Apr, 2011 14:16:03

Message: 8 of 22



"dpb" <none@non.net> wrote in message news:iosuu8$sg6$1@speranza.aioe.org...
> On 4/22/2011 5:07 PM, Ali wrote:
>> "Sprinceana " <mihaispr@yahoo.com> wrote in message
>> <h2s4c1$3aj$1@fred.mathworks.com>...
>>> I have the matrix:
>>>
>>> A=[1 2 3;4 5 6; 6 7 8]
>>>
>>> And I want to obtain the vector line:
>>>
>>> 1 2 3 4 5 6 7 8
> ...
>
>>> Is there another method to obtain that? I wait your advices!
>>
>>
>> B=reshape(A',1,9) does it for you!
>
> Indeed; too bad that Matlab doesn't allow
>
> v=A'(:)';
>
> to eliminate the explicit reshape() call...closest one can come is
>
> v=A';v=v(:)'

If you really MUST have it in one line without an explicit RESHAPE:

% Assuming the OP's matrix and their expected vector have typos, the sample
data is:
A = [1 2 3;4 5 6; 7 8 9];

A2 = subsref(A.', substruct('()', {':'})).'

But to some that may be less understandable than the simple RESHAPE version.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Transform matrix to vector

From: dpb

Date: 25 Apr, 2011 14:45:49

Message: 9 of 22

On 4/25/2011 9:16 AM, Steven_Lord wrote:
> "dpb" <none@non.net> wrote in message
> news:iosuu8$sg6$1@speranza.aioe.org...
...

>> Indeed; too bad that Matlab doesn't allow
>>
>> v=A'(:)';
>>
>> to eliminate the explicit reshape() call...closest one can come is
>>
>> v=A';v=v(:)'
>
> If you really MUST have it in one line without an explicit RESHAPE:
>
> % Assuming the OP's matrix and their expected vector have typos, the
> sample data is:
> A = [1 2 3;4 5 6; 7 8 9];
>
> A2 = subsref(A.', substruct('()', {':'})).'
>
> But to some that may be less understandable than the simple RESHAPE
> version.

Indeed; I'd take reshape()... :)

--

Subject: Transform matrix to vector

From: Shooga

Date: 26 Sep, 2013 11:14:05

Message: 10 of 22

Ok, that's clear ...
But If we have a matrix (3000 X 200), how to do it using a loop ?
I have tried, but the results were not right.

"Sprinceana " <mihaispr@yahoo.com> wrote in message <h2s4c1$3aj$1@fred.mathworks.com>...
> I have the matrix:
>
> A=[1 2 3;4 5 6; 6 7 8]
>
> And I want to obtain the vector line:
>
> 1 2 3 4 5 6 7 8
>
> What I did:
>
> >>A = [1 2 3;4 5 6; 6 7 8] % matrix definition
> >>b=A(1,:) % extract line 1 of matrix A in vector b
> >>c=A(2,:) % extract line 2 of matrix A in vector c
> >>d=A(3,:) % % extract line 3 of matrix A in vector d
> >>result=[b c d] %concatenate the 3 vectors b,c,d in a single vector result
> >>unique(result) % eliminate doubled elements from vector result
>
> Is there another method to obtain that? I wait your advices!

Subject: Transform matrix to vector

From: Steven_Lord

Date: 26 Sep, 2013 13:54:35

Message: 11 of 22



"Shooga " <www.ahmed_nehad@hotmail.com> wrote in message
news:l214tt$r79$1@newscl01ah.mathworks.com...
> Ok, that's clear ...
> But If we have a matrix (3000 X 200), how to do it using a loop ?
> I have tried, but the results were not right.

You want the unique elements in that matrix?

A = randi([1 100], 3000, 200);
uniqueElements = unique(A(:));

Don't use a loop here.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Transform matrix to vector

From: Horia

Date: 11 May, 2014 19:41:09

Message: 12 of 22

"Steven_Lord" <slord@mathworks.com> wrote in message <l21eas$sui$1@newscl01ah.mathworks.com>...
>
>
> "Shooga " <www.ahmed_nehad@hotmail.com> wrote in message
> news:l214tt$r79$1@newscl01ah.mathworks.com...
> > Ok, that's clear ...
> > But If we have a matrix (3000 X 200), how to do it using a loop ?
> > I have tried, but the results were not right.
>
> You want the unique elements in that matrix?
>
> A = randi([1 100], 3000, 200);
> uniqueElements = unique(A(:));
>
> Don't use a loop here.
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com


Given a m x n matrix I:
[sz1,sz2]=size(I);
J=reshape(I,1,sz1*sz2);

Subject: Transform matrix to vector

From: Horia

Date: 11 May, 2014 19:41:09

Message: 13 of 22

"Steven_Lord" <slord@mathworks.com> wrote in message <l21eas$sui$1@newscl01ah.mathworks.com>...
>
>
> "Shooga " <www.ahmed_nehad@hotmail.com> wrote in message
> news:l214tt$r79$1@newscl01ah.mathworks.com...
> > Ok, that's clear ...
> > But If we have a matrix (3000 X 200), how to do it using a loop ?
> > I have tried, but the results were not right.
>
> You want the unique elements in that matrix?
>
> A = randi([1 100], 3000, 200);
> uniqueElements = unique(A(:));
>
> Don't use a loop here.
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

Subject: Transform matrix to vector

From: dpb

Date: 11 May, 2014 19:49:16

Message: 14 of 22

On 5/11/2014 2:41 PM, Horia wrote:
...

> Given a m x n matrix I:
> [sz1,sz2]=size(I);
> J=reshape(I,1,sz1*sz2);

While the original thread is ancient and has disappeared from my
newsserver it appears was actually looking for the unique() members of a
vector given Steven's response.

But, for the above, all one needs is

J=I(:); % column vector or
J=I(:).'; % row vector

size() and reshape() are overkill...

Also, in such a case if were to use reshape(), the better form would be

J=reshape(I,1,[]); % use default trailing dimension size

--

Subject: Transform matrix to vector

From: Fabrizio Schiano

Date: 25 Nov, 2014 09:22:09

Message: 15 of 22

EASY REPLY:

Do you want to transform a Matrix
A=[1 2 3;4 5 6;7 8 9;10 11 12]
 
into a vector v= [1;2;3;4;5;6;7;8;9;10;11;12] ?

To do that just: v=A(:)

That's it.

Subject: Transform matrix to vector

From: Bruno Luong

Date: 25 Nov, 2014 14:00:11

Message: 16 of 22

"Fabrizio Schiano" <fabrizio.schiano@gmail.com> wrote in message <m51ho1$ifa$1@newscl01ah.mathworks.com>...
> EASY REPLY:
>
> Do you want to transform a Matrix
> A=[1 2 3;4 5 6;7 8 9;10 11 12]
>
> into a vector v= [1;2;3;4;5;6;7;8;9;10;11;12] ?
>
> To do that just: v=A(:)

This gives columnwise arrangement, not rowwise

Bruno

Subject: Transform matrix to vector

From: dpb

Date: 25 Nov, 2014 14:06:24

Message: 17 of 22

On 11/25/2014 8:00 AM, Bruno Luong wrote:
> "Fabrizio Schiano" <fabrizio.schiano@gmail.com> wrote in message
> <m51ho1$ifa$1@newscl01ah.mathworks.com>...
>> EASY REPLY:
>>
>> Do you want to transform a Matrix A=[1 2 3;4 5 6;7 8 9;10 11 12]
>>
>> into a vector v= [1;2;3;4;5;6;7;8;9;10;11;12] ?
>>
>> To do that just: v=A(:)
>
> This gives columnwise arrangement, not rowwise

Then

v=A(:).';

--

Subject: Transform matrix to vector

From: Bruno Luong

Date: 25 Nov, 2014 15:12:06

Message: 18 of 22

"dpb" wrote in message <m522ck$b50$1@dont-email.me>...

>
> Then
>
> v=A(:).';
>

Please think again...

Bruno

Subject: Transform matrix to vector

From: dpb

Date: 25 Nov, 2014 16:07:42

Message: 19 of 22

On 11/25/2014 9:12 AM, Bruno Luong wrote:
> "dpb" wrote in message <m522ck$b50$1@dont-email.me>...
>
>>
>> Then
>>
>> v=A(:).';
>>
>
> Please think again...

What's to think?

 >> a=rand(2);
 >> a(:)
ans =
     0.8147
     0.9058
     0.1270
     0.9134
 >> a(:).'
ans =
     0.8147 0.9058 0.1270 0.9134
 >>

Transposing a column vector gives a row vector...

--

Subject: Transform matrix to vector

From: Bruno Luong

Date: 25 Nov, 2014 16:34:06

Message: 20 of 22

"dpb" wrote in message <m529g2$bb6$1@dont-email.me>...
> On 11/25/2014 9:12 AM, Bruno Luong wrote:
> > "dpb" wrote in message <m522ck$b50$1@dont-email.me>...
> >
> >>
> >> Then
> >>
> >> v=A(:).';
> >>
> >
> > Please think again...
>
> What's to think?

Have you read the question on the very first post?: quote

[
I have the matrix:

A=[1 2 3;4 5 6; 6 7 8]

And I want to obtain the vector line:

1 2 3 4 5 6 7 8
]

Subject: Transform matrix to vector

From: dpb

Date: 25 Nov, 2014 18:47:21

Message: 21 of 22

On 11/25/2014 10:34 AM, Bruno Luong wrote:
...

> Have you read the question on the very first post?: quote
...

Nope, only the subthread to which I responded showed up here...simply
commented on the query/comment re: row vs column vector.

--

Subject: Transform matrix to vector

From: Bruno Luong

Date: 25 Nov, 2014 18:55:06

Message: 22 of 22

"dpb" wrote in message <m52ird$iuq$1@dont-email.me>...
> On 11/25/2014 10:34 AM, Bruno Luong wrote:
> ...
>
> > Have you read the question on the very first post?: quote
> ...
>
> Nope, only the subthread to which I responded showed up here...simply
> commented on the query/comment re: row vs column vector.

When I wrote "rowwise", it concerns the order of the source matrix not the end result.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us