Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: Faster, more efficient way of doing this?

Subject: Faster, more efficient way of doing this?

From: Syang

Date: 11 Jul, 2008 05:12:02

Message: 1 of 8

Hello everyone.

I'm still learning Matlab and I was wondering if there is
better way of doing this specific task:

I want to transform an ordered row vector into a matrix in
which each row is a predetermined-sized partition of the
initial vector.

So for instance, let's say the initial vector is:

s = [1,4,5,6,7,8]

I want to obtain a 2x3 matrix t of this form:

t = [1 4 5
     6 7 8]

So far I have come up with this:

i = 1;
while i <= length(s)
      for k =1:4
        for j = 1:3
          t(k,j) = s(1,i);
        i = i+1;
      end
      end
end

which seems to work.

Are there Matlab-specific operators/functions that can
accomplish this in a faster and more efficient way?

Subject: Faster, more efficient way of doing this?

From: Syang

Date: 11 Jul, 2008 05:21:02

Message: 2 of 8

Sorry, for that particular example I gave, the code should be:

i = 1;
while i <= length(s)
       for k =1:2
         for j = 1:3
           t(k,j) = s(1,i);
         i = i+1;
       end
       end
 end


I copy-pasted the initial example which was designed for an
s vector of length 12 and t of rows=1:4,cols=1:3.

Subject: Faster, more efficient way of doing this?

From: Nitin Chhabra

Date: 11 Jul, 2008 05:56:02

Message: 3 of 8

"Syang " <partner55749338@aravensoft.com> wrote in message
<g56qju$qm1$1@fred.mathworks.com>...
> Sorry, for that particular example I gave, the code
should be:
>
> i = 1;
> while i <= length(s)
> for k =1:2
> for j = 1:3
> t(k,j) = s(1,i);
> i = i+1;
> end
> end
> end
>
>
> I copy-pasted the initial example which was designed for
an
> s vector of length 12 and t of rows=1:4,cols=1:3.

Hi,

'reshape' will help you.
>> a
a =
     1 2 3 4 5 6
>> reshape (a,2,3)
ans =
     1 3 5
     2 4 6

with regards,
Nitin

Subject: Faster, more efficient way of doing this?

From: Syang

Date: 11 Jul, 2008 06:16:02

Message: 4 of 8

No ,unfortunately that won't work.

Reshape generates this:

ans =

     1 3 5
     2 4 6

But I need it in this format:

     1 2 3
     4 5 6

This is different than what the standard reshape command does.

Subject: Faster, more efficient way of doing this?

From: Nitin Chhabra

Date: 11 Jul, 2008 06:27:02

Message: 5 of 8

"Syang " <partner55749338@aravensoft.com> wrote in message
<g56tr2$bdd$1@fred.mathworks.com>...
> No ,unfortunately that won't work.
>
> Reshape generates this:
>
> ans =
>
> 1 3 5
> 2 4 6
>
> But I need it in this format:
>
> 1 2 3
> 4 5 6
>
> This is different than what the standard reshape command
does.
>
Hi,

You can use following command :

reshape(a,3,2)'

Just a small variant.

with regards,
Nitin

Subject: Faster, more efficient way of doing this?

From: Eric

Date: 11 Jul, 2008 06:31:02

Message: 6 of 8

reshape(a, 3, 2)'

Subject: Faster, more efficient way of doing this?

From: Syang

Date: 11 Jul, 2008 06:40:05

Message: 7 of 8

It works! Thank you guys!

Subject: Faster, more efficient way of doing this?

From: us

Date: 11 Jul, 2008 06:46:02

Message: 8 of 8

"Syang ":
<SNIP reshape by column...

> s = [1,4,5,6,7,8]
> I want to obtain a 2x3 matrix t of this form:
> t = [1 4 5
> 6 7 8]

in addition to what others have already tried to tell you,
there's a small utility on the fex that will do a column-
wise reshape for all kinds of input

reshapec.m, at

http://www.mathworks.com/matlabcentral/fileexchange/loadFile
.do?objectId=20255&objectType=FILE

% eg,
     v=[1,4,5,6,7,8];
     r=reshapec(v,[2,3])
%{
     r =
     1 4 5
     6 7 8
%}

us

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
reference us 11 Jul, 2008 02:50:07
fex us 11 Jul, 2008 02:50:07
file exchange us 11 Jul, 2008 02:50:07
reshapec us 11 Jul, 2008 02:50:07
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics