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 extract series from a matrix

Subject: How to extract series from a matrix

From: Kfupm engsub

Date: 29 Dec, 2011 19:37:08

Message: 1 of 5


Hi,

I have a very large matrix. A simple example is:

Suppose I have this matrix:


M= [50 55 52 51
       20 21 24 23
        1 1 2 2
        1 2 1 2
        89 88 80 82];

I want to construct a new matrix that gathers every two columns

by rowise following in one vector. So, the desired matrix is:


A= [50 52
       55 51
       20 24
       21 23
        1 2
        1 2
        1 1
        2 2
       89 80
       88 82] ;

I tried using reshape but I couln't get the desired output.

regards

Subject: How to extract series from a matrix

From: Roger Stafford

Date: 31 Dec, 2011 07:05:09

Message: 2 of 5

"Kfupm engsub" wrote in message <jdifh4$s4r$1@newscl01ah.mathworks.com>...
> I have a very large matrix. A simple example is:
> Suppose I have this matrix:
>
> M= [50 55 52 51
> 20 21 24 23
> 1 1 2 2
> 1 2 1 2
> 89 88 80 82];
>
> I want to construct a new matrix that gathers every two columns
> by rowise following in one vector. So, the desired matrix is:
>
> A= [50 52
> 55 51
> 20 24
> 21 23
> 1 2
> 1 2
> 1 1
> 2 2
> 89 80
> 88 82] ;
- - - - - - - - - -
  Try this horror:

 [p,q] = size(M); % The number of columns, q, must be even
 n = 0:p*q-1;
 A = reshape(M((2*mod(n,2*p)-(2*p-1)*(-1).^n+2*p+3)/4+2*p*floor(n/2/p)),2*p,q/2);

Roger Stafford

Subject: How to extract series from a matrix

From: Nasser M. Abbasi

Date: 31 Dec, 2011 08:44:19

Message: 3 of 5

On 12/31/2011 1:05 AM, Roger Stafford wrote:
> "Kfupm engsub" wrote in message<jdifh4$s4r$1@newscl01ah.mathworks.com>...
>> I have a very large matrix. A simple example is:
>> Suppose I have this matrix:
>>
>> M= [50 55 52 51
>> 20 21 24 23
>> 1 1 2 2
>> 1 2 1 2
>> 89 88 80 82];
>>
>> I want to construct a new matrix that gathers every two columns
>> by rowise following in one vector. So, the desired matrix is:
>>
>> A= [50 52
>> 55 51
>> 20 24
>> 21 23
>> 1 2
>> 1 2
>> 1 1
>> 2 2
>> 89 80
>> 88 82] ;
> - - - - - - - - - -
> Try this horror:
>
> [p,q] = size(M); % The number of columns, q, must be even
> n = 0:p*q-1;
> A = reshape(M((2*mod(n,2*p)-(2*p-1)*(-1).^n+2*p+3)/4+2*p*floor(n/2/p)),2*p,q/2);
>
> Roger Stafford

wow, I am not going to figure how you came up with this one Roger ;)

How about one with few more lines? This is my attempt: It assumes
also even rows/columns.

-----------------------------------
clear all;

M= [50 55 52 51
      20 21 24 23
      1 1 2 2
      1 2 1 2
      89 88 80 82];

  
[p,q]=size(M);
np=p*q/2;
B=zeros(np,2); %make storage for result

%make the 2 sub matrices
m1=reshape(M(:,1:2:q),[np/2,2]);
m2=reshape(M(:,2:2:q),[np/2,2]);

%stuff them into the result matrix
B(1:2:np,:)=m1;
B(2:2:np,:)=m2;
--------------------------

B =

     50 52
     55 51
     20 24
     21 23
      1 2
      1 2
      1 1
      2 2
     89 80
     88 82

--Nasser

Subject: How to extract series from a matrix

From: Bruno Luong

Date: 31 Dec, 2011 09:55:08

Message: 4 of 5

Yet another solution:

p = size(M,1);
reshape(permute(reshape(M,p,2,[]),[2 1 3]),p*2,[])

% Bruno

Subject: How to extract series from a matrix

From: Roger Stafford

Date: 31 Dec, 2011 21:31:08

Message: 5 of 5

"Nasser M. Abbasi" <nma@12000.org> wrote in message <jdmi16$452$1@speranza.aioe.org>...
> wow, I am not going to figure how you came up with this one Roger ;)
- - - - - - - - - - -
  Hi Nassar. That expression I gave is not as unnatural as one might first think. With systematic rearrangements like the current one, if one views the linear index permutation that is to be achieved, these usually are composed of just three kinds of components, a linear function of the index, a "saw-tooth" component best generated with the 'mod' function (or in my case powers of minus one - I could just as well have used modulo 2,) and a "staircase" component for which the 'floor' of a linear index combination is admirably suited. All that need be determined are formulas for the necessary coefficients. That was quite easily done in this case.

  This is not to say that such a solution is necessarily computationally efficient. Probably your and Bruno's codes are much faster than mine. However, such a solution can be useful if nothing else comes to mind, since it usually can be realized without having to come up with any particularly brilliant ideas. Actually Mathworks' realization of the 'transpose' and 'permute' operations must involve some numerical procedures which have a floor-like equivalent at the low compiler level.

Roger Stafford

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