Got Questions? Get Answers.
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:
how to convert matrices to vectors

Subject: how to convert matrices to vectors

From: nan yan

Date: 23 Jun, 2009 17:45:03

Message: 1 of 12

hello everyone,
i have a matrices A=210 by 4 , i want to split erery row as a vector (1 by 4) then group 6 rows as a new vector (so, there will be a new matrices 35 by 6).
here is a part of my code:
%%%%%%%%%%%%%%
for i=1:840
    subcarriers(i,:)=i;
end
for i=1:210
    j=1:4;
       physical_Tile(i,j)=subcarriers(4*(i-1)+j);
end
% i want ..
N_subchannel=35by6 (from the physical_Tile)
%%%%%%%%%%
to make it be 35 by 6

Subject: how to convert matrices to vectors

From: Jos

Date: 23 Jun, 2009 18:08:01

Message: 2 of 12

"nan yan" <njyan2005@yahoo.co.uk> wrote in message <h1r4av$ks8$1@fred.mathworks.com>...
> hello everyone,
> i have a matrices A=210 by 4 , i want to split erery row as a vector (1 by 4) then group 6 rows as a new vector (so, there will be a new matrices 35 by 6).
> here is a part of my code:
> %%%%%%%%%%%%%%
> for i=1:840
> subcarriers(i,:)=i;
> end
> for i=1:210
> j=1:4;
> physical_Tile(i,j)=subcarriers(4*(i-1)+j);
> end
> % i want ..
> N_subchannel=35by6 (from the physical_Tile)
> %%%%%%%%%%
> to make it be 35 by 6

I am unsure what you want, but I suggest you take a look at reshape and transpose. Perhaps you can give a small example with the input and expected output?

Jos

Subject: how to convert matrices to vectors

From: nan yan

Date: 23 Jun, 2009 20:15:03

Message: 3 of 12

hello jos,
sorry for my unclear question, here is an example i want:
%%%%%%%%%%%%%%
my inputs: (210,4) as:
     1 2 3 4
     5 6 7 8
     9 ......
     .
     .
     837 838 839 840
%%
i want the index 1,2,3,4 as a vector 1, and 5,6,7,8 as vector 2, and so on. after that, i want to make vector 1 to vector 6 as a subchannel 1, and vector 7 to vector 12 as subchannel 2, and so on, so the finally will be somthing looks like this:
subchannel 1 1 2 3 4 5 6 7 8 9 10......24
subchannel 2 25 26 27 .........
        .
        .
subchannel 35 ........................839 840

i holp this will make clear about my question,
if you need more please let me know.

thank you
nan

Subject: how to convert matrices to vectors

From: Nathan

Date: 23 Jun, 2009 20:32:12

Message: 4 of 12

On Jun 23, 1:15 pm, "nan yan" <njyan2...@yahoo.co.uk> wrote:
> hello jos,
> sorry for my unclear question, here is an example i want:
> %%%%%%%%%%%%%%
> my inputs: (210,4) as:
>      1     2     3     4
>      5     6     7     8
>      9 ......
>      .
>      .
>      837 838 839 840
> %%
> i want the index 1,2,3,4 as a vector 1, and 5,6,7,8 as vector 2, and so on. after that, i want to make vector 1 to vector 6 as a subchannel 1, and vector 7 to vector 12 as subchannel 2, and so on, so the finally will be somthing looks like this:
> subchannel 1         1 2 3 4 5 6 7 8 9 10......24
> subchannel 2         25 26 27 .........
>         .
>         .
> subchannel 35        ........................839  840
>
> i holp this will make clear about my question,
> if you need more please let me know.
>
> thank you
> nan

I'm using A as your (210,4) matrix.

B = [];
for i=1:35
  B = [B;A(i*1,:) A(i*2,:) A(i*3,:) A(i*4,:) A(i*5,:) A(i*6,:)];
end

It works, but probably isn't the most efficient.
Hope this is what you were looking for.
-Nathan

Subject: how to convert matrices to vectors

From: Jos

Date: 23 Jun, 2009 20:54:02

Message: 5 of 12

"nan yan" <njyan2005@yahoo.co.uk> wrote in message <h1rd47$s90$1@fred.mathworks.com>...
> hello jos,
> sorry for my unclear question, here is an example i want:
> %%%%%%%%%%%%%%
> my inputs: (210,4) as:
> 1 2 3 4
> 5 6 7 8
> 9 ......
> .
> .
> 837 838 839 840
> %%
> i want the index 1,2,3,4 as a vector 1, and 5,6,7,8 as vector 2, and so on. after that, i want to make vector 1 to vector 6 as a subchannel 1, and vector 7 to vector 12 as subchannel 2, and so on, so the finally will be somthing looks like this:
> subchannel 1 1 2 3 4 5 6 7 8 9 10......24
> subchannel 2 25 26 27 .........
> .
> .
> subchannel 35 ........................839 840
>
> i holp this will make clear about my question,
> if you need more please let me know.
>
> thank you
> nan

A typical job for RESHAPE and TRANSPOSE, indeed ...

% your data
  A = reshape(1:840,4,[]).' ; % ;-)

% to obtain a specific vector use straightforward indexing, for instance
 vector(1,:)
 vector(end,:)

% to obtain a specific channel:
  subchannel = reshape(A.', 6 * size(A,2),[]).'
  % and index into this matrix
  subchannel(1,:)
  subchannel(end,:)

hth
Jos

Subject: how to convert matrices to vectors

From: nan yan

Date: 23 Jun, 2009 21:00:04

Message: 6 of 12

Nathan <ngreco32@gmail.com> wrote in message <26eb1d7a-82a8-4304-8008-48aadb4f9aaa@c18g2000prh.googlegroups.com>...
> On Jun 23, 1:15?pm, "nan yan" <njyan2...@yahoo.co.uk> wrote:
> > hello jos,
> > sorry for my unclear question, here is an example i want:
> > %%%%%%%%%%%%%%
> > my inputs: (210,4) as:
> > ? ? ?1 ? ? 2 ? ? 3 ? ? 4
> > ? ? ?5 ? ? 6 ? ? 7 ? ? 8
> > ? ? ?9 ......
> > ? ? ?.
> > ? ? ?.
> > ? ? ?837 838 839 840
> > %%
> > i want the index 1,2,3,4 as a vector 1, and 5,6,7,8 as vector 2, and so on. after that, i want to make vector 1 to vector 6 as a subchannel 1, and vector 7 to vector 12 as subchannel 2, and so on, so the finally will be somthing looks like this:
> > subchannel 1 ? ? ? ? 1 2 3 4 5 6 7 8 9 10......24
> > subchannel 2 ? ? ? ? 25 26 27 .........
> > ? ? ? ? .
> > ? ? ? ? .
> > subchannel 35 ? ? ? ?........................839 ?840
> >
> > i holp this will make clear about my question,
> > if you need more please let me know.
> >
> > thank you
> > nan
>
> I'm using A as your (210,4) matrix.
>
> B = [];
> for i=1:35
> B = [B;A(i*1,:) A(i*2,:) A(i*3,:) A(i*4,:) A(i*5,:) A(i*6,:)];
> end
>
> It works, but probably isn't the most efficient.
> Hope this is what you were looking for.
> -Nathan

hello Nathan,

thank you for you suggestion, but your index from 2nd subchannel are not same as i described above.

i was wander, can i split the (210,4) matrices to 210 vectors then group to 35 subchannels, where every subchannel content 6 vectors. but i dont know how to do that!

thank you
nan

Subject: how to convert matrices to vectors

From: Nathan

Date: 23 Jun, 2009 21:16:14

Message: 7 of 12

On Jun 23, 2:00 pm, "nan yan" <njyan2...@yahoo.co.uk> wrote:
> Nathan <ngrec...@gmail.com> wrote in message <26eb1d7a-82a8-4304-8008-48aadb4f9...@c18g2000prh.googlegroups.com>...
> > On Jun 23, 1:15?pm, "nan yan" <njyan2...@yahoo.co.uk> wrote:
> > > hello jos,
> > > sorry for my unclear question, here is an example i want:
> > > %%%%%%%%%%%%%%
> > > my inputs: (210,4) as:
> > > ? ? ?1 ? ? 2 ? ? 3 ? ? 4
> > > ? ? ?5 ? ? 6 ? ? 7 ? ? 8
> > > ? ? ?9 ......
> > > ? ? ?.
> > > ? ? ?.
> > > ? ? ?837 838 839 840
> > > %%
> > > i want the index 1,2,3,4 as a vector 1, and 5,6,7,8 as vector 2, and so on. after that, i want to make vector 1 to vector 6 as a subchannel 1, and vector 7 to vector 12 as subchannel 2, and so on, so the finally will be somthing looks like this:
> > > subchannel 1 ? ? ? ? 1 2 3 4 5 6 7 8 9 10......24
> > > subchannel 2 ? ? ? ? 25 26 27 .........
> > > ? ? ? ? .
> > > ? ? ? ? .
> > > subchannel 35 ? ? ? ?........................839 ?840
>
> > > i holp this will make clear about my question,
> > > if you need more please let me know.
>
> > > thank you
> > > nan
>
> > I'm using A as your (210,4) matrix.
>
> > B = [];
> > for i=1:35
> >   B = [B;A(i*1,:) A(i*2,:) A(i*3,:) A(i*4,:) A(i*5,:) A(i*6,:)];
> > end
>
> > It works, but probably isn't the most efficient.
> > Hope this is what you were looking for.
> > -Nathan
>
> hello Nathan,
>
> thank you for you suggestion, but  your index from 2nd subchannel are not same as i described above.
>
> i was wander, can i split the (210,4) matrices to 210 vectors then group to 35 subchannels, where every subchannel content 6 vectors. but i dont know how to do that!
>
> thank you
> nan

Ah, you're right. I was too quick to draw a conclusion.
I mixed some terms up... Here is mine again, and this time it should
work... but I think Jos has a better answer.

(again, A is your (210,4) matrix)
B = [];
for i=1:35
  B = [B;A(6*(i-1)+1,:) A(6*(i-1)+2,:) A(6*(i-1)+3,:) A(6*(i-1)+4,:) A
(6*(i-1)+5,:) A(6*(i-1)+6,:)];
end

Subject: how to convert matrices to vectors

From: Faraz Afzal

Date: 23 Jun, 2009 21:32:01

Message: 8 of 12

There we go.. As far as i understood you want this... Dont forget to say thanx if it works ..:o))

A = 210 by 4 matrix

splitting every row as a vector..
for k = 1:210
 B{k} = reshape(A(k,:),1,4 )
end
 
The above thing will make 210 vectors.. B{k} is the index..

Next task.I am gonna assume when u say vector u need a column matrix not a row matrix as most of the people assumes...


subchannel {1} = [(B{1})' ; (B{2})' ; (B{3})' ; (B{4})' ; (B{5})' (B{6})']

and keep on increasing the index and u will get ur subchannels...
in case u want sub channels as row vectors u just need to say subchannel{1}= subchannel{1}' (that is transpose...)....

I hope that helps...

"nan yan" <njyan2005@yahoo.co.uk> wrote in message <h1r4av$ks8$1@fred.mathworks.com>...
> hello everyone,
> i have a matrices A=210 by 4 , i want to split erery row as a vector (1 by 4) then group 6 rows as a new vector (so, there will be a new matrices 35 by 6).
> here is a part of my code:
> %%%%%%%%%%%%%%
> for i=1:840
> subcarriers(i,:)=i;
> end
> for i=1:210
> j=1:4;
> physical_Tile(i,j)=subcarriers(4*(i-1)+j);
> end
> % i want ..
> N_subchannel=35by6 (from the physical_Tile)
> %%%%%%%%%%
> to make it be 35 by 6

Subject: how to convert matrices to vectors

From: nan yan

Date: 23 Jun, 2009 22:14:02

Message: 9 of 12

"Faraz Afzal" <farazafzal@gmail.com> wrote in message <h1rhkh$ntb$1@fred.mathworks.com>...
> There we go.. As far as i understood you want this... Dont forget to say thanx if it works ..:o))
>
> A = 210 by 4 matrix
>
> splitting every row as a vector..
> for k = 1:210
> B{k} = reshape(A(k,:),1,4 )
> end
>
> The above thing will make 210 vectors.. B{k} is the index..
>
> Next task.I am gonna assume when u say vector u need a column matrix not a row matrix as most of the people assumes...
>
>
> subchannel {1} = [(B{1})' ; (B{2})' ; (B{3})' ; (B{4})' ; (B{5})' (B{6})']
>
> and keep on increasing the index and u will get ur subchannels...
> in case u want sub channels as row vectors u just need to say subchannel{1}= subchannel{1}' (that is transpose...)....
>
> I hope that helps...
>
> "nan yan" <njyan2005@yahoo.co.uk> wrote in message <h1r4av$ks8$1@fred.mathworks.com>...
> > hello everyone,
> > i have a matrices A=210 by 4 , i want to split erery row as a vector (1 by 4) then group 6 rows as a new vector (so, there will be a new matrices 35 by 6).
> > here is a part of my code:
> > %%%%%%%%%%%%%%
> > for i=1:840
> > subcarriers(i,:)=i;
> > end
> > for i=1:210
> > j=1:4;
> > physical_Tile(i,j)=subcarriers(4*(i-1)+j);
> > end
> > % i want ..
> > N_subchannel=35by6 (from the physical_Tile)
> > %%%%%%%%%%
> > to make it be 35 by 6

yes, Faraz Afzal, you are absolutly right on my thought. thank you very much indeed! of cause also thanks to Jos and Nathan.
but, this bit subchannel{1}= subchannel{1}' it doesn't change. i do this:

for k=1:35
subchannel {k} = [(B{1})' ; (B{2})' ; (B{3})' ; (B{4})' ; (B{5})'; (B{6})'];
end
subchannel{k}= subchannel{k}'
yes, i do want to have 35 rows and 6 columns! sorry



thank you all
nan

Subject: how to convert matrices to vectors

From: Messam Abbas

Date: 23 Jun, 2009 22:26:01

Message: 10 of 12

Dear Nan

It indeed works,,

subchannel{1} = subchannel{1}'

u gotta copy paste the thing above.. and then write

subchannel{1}

and u gonna see 1 row 24 columns...

These are the cellls and to view the cells u have to give there index..
 try this.. and when u r done.. do inform...

Regards,
Happy MATLAB. :o)
Faraz

"nan yan" <njyan2005@yahoo.co.uk> wrote in message <h1rk3a$3bs$1@fred.mathworks.com>...
> "Faraz Afzal" <farazafzal@gmail.com> wrote in message <h1rhkh$ntb$1@fred.mathworks.com>...
> > There we go.. As far as i understood you want this... Dont forget to say thanx if it works ..:o))
> >
> > A = 210 by 4 matrix
> >
> > splitting every row as a vector..
> > for k = 1:210
> > B{k} = reshape(A(k,:),1,4 )
> > end
> >
> > The above thing will make 210 vectors.. B{k} is the index..
> >
> > Next task.I am gonna assume when u say vector u need a column matrix not a row matrix as most of the people assumes...
> >
> >
> > subchannel {1} = [(B{1})' ; (B{2})' ; (B{3})' ; (B{4})' ; (B{5})' (B{6})']
> >
> > and keep on increasing the index and u will get ur subchannels...
> > in case u want sub channels as row vectors u just need to say subchannel{1}= subchannel{1}' (that is transpose...)....
> >
> > I hope that helps...
> >
> > "nan yan" <njyan2005@yahoo.co.uk> wrote in message <h1r4av$ks8$1@fred.mathworks.com>...
> > > hello everyone,
> > > i have a matrices A=210 by 4 , i want to split erery row as a vector (1 by 4) then group 6 rows as a new vector (so, there will be a new matrices 35 by 6).
> > > here is a part of my code:
> > > %%%%%%%%%%%%%%
> > > for i=1:840
> > > subcarriers(i,:)=i;
> > > end
> > > for i=1:210
> > > j=1:4;
> > > physical_Tile(i,j)=subcarriers(4*(i-1)+j);
> > > end
> > > % i want ..
> > > N_subchannel=35by6 (from the physical_Tile)
> > > %%%%%%%%%%
> > > to make it be 35 by 6
>
> yes, Faraz Afzal, you are absolutly right on my thought. thank you very much indeed! of cause also thanks to Jos and Nathan.
> but, this bit subchannel{1}= subchannel{1}' it doesn't change. i do this:
>
> for k=1:35
> subchannel {k} = [(B{1})' ; (B{2})' ; (B{3})' ; (B{4})' ; (B{5})'; (B{6})'];
> end
> subchannel{k}= subchannel{k}'
> yes, i do want to have 35 rows and 6 columns! sorry
>
>
>
> thank you all
> nan

Subject: how to convert matrices to vectors

From: Matt Fig

Date: 23 Jun, 2009 22:27:02

Message: 11 of 12

There is no need for all this looping. Use Jos's idea:

% The data:
A = reshape(1:840,4,210).';

% The engine:
B = reshape(A.',24,35).';
subchannel = mat2cell(B,ones(35,1),24);

% Now look:
subchannel{1}

Subject: how to convert matrices to vectors

From: nan yan

Date: 23 Jun, 2009 22:51:01

Message: 12 of 12

"Matt Fig" <spamanon@yahoo.com> wrote in message <h1rkrm$m6n$1@fred.mathworks.com>...
> There is no need for all this looping. Use Jos's idea:
>
> % The data:
> A = reshape(1:840,4,210).';
>
> % The engine:
> B = reshape(A.',24,35).';
> subchannel = mat2cell(B,ones(35,1),24);
>
> % Now look:
> subchannel{1}

yes, Matt, thank you, you solution is better, and realy simple,faster.

thank you a lot
nan

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