Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to extract series from a matrix

0 views
Skip to first unread message

Kfupm engsub

unread,
Dec 29, 2011, 2:37:08 PM12/29/11
to

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

Roger Stafford

unread,
Dec 31, 2011, 2:05:09 AM12/31/11
to
"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

Nasser M. Abbasi

unread,
Dec 31, 2011, 3:44:19 AM12/31/11
to
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

Bruno Luong

unread,
Dec 31, 2011, 4:55:08 AM12/31/11
to
Yet another solution:

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

% Bruno

Roger Stafford

unread,
Dec 31, 2011, 4:31:08 PM12/31/11
to
"Nasser M. Abbasi" <n...@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
0 new messages