Thread Subject: Splitting a long vector into matrix of variable columns

Subject: Splitting a long vector into matrix of variable columns

From: Mark

Date: 13 Feb, 2011 10:30:06

Message: 1 of 15

Hello. I wish to split a very long vector into a single matrix where the rows consist of values between a certain interval. This interval is defined usinga for loop. I give a simplified example but I'm unsure of the coding. I think the problem is in the last 2 lines where I introduce the H8j).

L=4;
        H=[1 1.5 2 3 4 5.5 6 7 8 9 10.5 11 12 13]';
        for i=1:length(H);
           x(1)=0;
            x(i)=1+x(i-1);
            y(i)=L+x(i);
            
            if H(j)>x(i) && H(j)<y(i);
            N=H(j); %Need a matrix with H(j) as rows
        end
        end

The first row of the matriy should read [1.5 2 3 4]. Any advice is appreciated.

Subject: Splitting a long vector into matrix of variable columns

From: Jeff

Date: 13 Feb, 2011 11:09:03

Message: 2 of 15

"Mark" wrote in message <ij8bre$9jk$1@fred.mathworks.com>...
> Hello. I wish to split a very long vector into a single matrix where the rows consist of values between a certain interval. This interval is defined usinga for loop. I give a simplified example but I'm unsure of the coding. I think the problem is in the last 2 lines where I introduce the H8j).
>
> L=4;
> H=[1 1.5 2 3 4 5.5 6 7 8 9 10.5 11 12 13]';
> for i=1:length(H);
> x(1)=0;
> x(i)=1+x(i-1);
> y(i)=L+x(i);
>
> if H(j)>x(i) && H(j)<y(i);
> N=H(j); %Need a matrix with H(j) as rows
> end
> end
>
> The first row of the matriy should read [1.5 2 3 4]. Any advice is appreciated.

Well, the code given tries to access x(0) on line 'x(i)=1+x(i-1);' the first time through the loop. There is no 0-based indexing in Matlab (unfortunately). You also have not set a value for 'j', so the 'if...' statement will abort. I tried the code using 'for i=2:length(H);...', but I cannot determine what value you need for 'j', and so cannot help further.

Anyway, there is a 'histc' command in Matlab which I think will do what you want much faster and easier. Look that up.

Subject: Splitting a long vector into matrix of variable columns

From: Franck Dernoncourt

Date: 13 Feb, 2011 11:18:04

Message: 3 of 15

"Mark" wrote in message <ij8bre$9jk$1@fred.mathworks.com>...
> Hello. I wish to split a very long vector into a single matrix where the rows consist of values between a certain interval. This interval is defined usinga for loop. I give a simplified example but I'm unsure of the coding. I think the problem is in the last 2 lines where I introduce the H8j).
>
> L=4;
> H=[1 1.5 2 3 4 5.5 6 7 8 9 10.5 11 12 13]';
> for i=1:length(H);
> x(1)=0;
> x(i)=1+x(i-1);
> y(i)=L+x(i);
>
> if H(j)>x(i) && H(j)<y(i);
> N=H(j); %Need a matrix with H(j) as rows
> end
> end
>
> The first row of the matriy should read [1.5 2 3 4]. Any advice is appreciated.

In the 1st iteration you're calling x(0) at line "x(i)=1+x(i-1);", which doesn't exist.

Subject: Splitting a long vector into matrix of variable columns

From: Mark

Date: 13 Feb, 2011 11:24:04

Message: 4 of 15

"Jeff" wrote in message <ij8e4f$4qk$1@fred.mathworks.com>...
> "Mark" wrote in message <ij8bre$9jk$1@fred.mathworks.com>...
> > Hello. I wish to split a very long vector into a single matrix where the rows consist of values between a certain interval. This interval is defined usinga for loop. I give a simplified example but I'm unsure of the coding. I think the problem is in the last 2 lines where I introduce the H8j).
> >
> > L=4;
> > H=[1 1.5 2 3 4 5.5 6 7 8 9 10.5 11 12 13]';
> > for i=1:length(H);
> > x(1)=0;
> > x(i)=1+x(i-1);
> > y(i)=L+x(i);
> >
> > if H(j)>x(i) && H(j)<y(i);
> > N=H(j); %Need a matrix with H(j) as rows
> > end
> > end
> >
> > The first row of the matriy should read [1.5 2 3 4]. Any advice is appreciated.
>
> Well, the code given tries to access x(0) on line 'x(i)=1+x(i-1);' the first time through the loop. There is no 0-based indexing in Matlab (unfortunately). You also have not set a value for 'j', so the 'if...' statement will abort. I tried the code using 'for i=2:length(H);...', but I cannot determine what value you need for 'j', and so cannot help further.
>
> Anyway, there is a 'histc' command in Matlab which I think will do what you want much faster and easier. Look that up.


Thaky you. Sorry the 'j' should signify any element of matrix H which meets the criteria. Perhaps j=1:length(H);.I wasnt sure how to do this. I will check the 'histc' function

Subject: Splitting a long vector into matrix of variable columns

From: Franck Dernoncourt

Date: 13 Feb, 2011 11:39:03

Message: 5 of 15

"Mark" wrote in message <ij8f0k$p1$1@fred.mathworks.com>...
> "Jeff" wrote in message <ij8e4f$4qk$1@fred.mathworks.com>...
> > "Mark" wrote in message <ij8bre$9jk$1@fred.mathworks.com>...
> > > Hello. I wish to split a very long vector into a single matrix where the rows consist of values between a certain interval. This interval is defined usinga for loop. I give a simplified example but I'm unsure of the coding. I think the problem is in the last 2 lines where I introduce the H8j).
> > >
> > > L=4;
> > > H=[1 1.5 2 3 4 5.5 6 7 8 9 10.5 11 12 13]';
> > > for i=1:length(H);
> > > x(1)=0;
> > > x(i)=1+x(i-1);
> > > y(i)=L+x(i);
> > >
> > > if H(j)>x(i) && H(j)<y(i);
> > > N=H(j); %Need a matrix with H(j) as rows
> > > end
> > > end
> > >
> > > The first row of the matriy should read [1.5 2 3 4]. Any advice is appreciated.
> >
> > Well, the code given tries to access x(0) on line 'x(i)=1+x(i-1);' the first time through the loop. There is no 0-based indexing in Matlab (unfortunately). You also have not set a value for 'j', so the 'if...' statement will abort. I tried the code using 'for i=2:length(H);...', but I cannot determine what value you need for 'j', and so cannot help further.
> >
> > Anyway, there is a 'histc' command in Matlab which I think will do what you want much faster and easier. Look that up.
>
>
> Thaky you. Sorry the 'j' should signify any element of matrix H which meets the criteria. Perhaps j=1:length(H);.I wasnt sure how to do this. I will check the 'histc' function

Sorry for the late post, I'm reading this ML through MATLAB Newsreader which is quite laggy...

Subject: Splitting a long vector into matrix of variable columns

From: Mark

Date: 13 Feb, 2011 12:33:03

Message: 6 of 15

"Franck Dernoncourt" <franck.dernoncourt@gmail.com> wrote in message <ij8fsn$q1r$1@fred.mathworks.com>...
> "Mark" wrote in message <ij8f0k$p1$1@fred.mathworks.com>...
> > "Jeff" wrote in message <ij8e4f$4qk$1@fred.mathworks.com>...
> > > "Mark" wrote in message <ij8bre$9jk$1@fred.mathworks.com>...
> > > > Hello. I wish to split a very long vector into a single matrix where the rows consist of values between a certain interval. This interval is defined usinga for loop. I give a simplified example but I'm unsure of the coding. I think the problem is in the last 2 lines where I introduce the H8j).
> > > >
> > > > L=4;
> > > > H=[1 1.5 2 3 4 5.5 6 7 8 9 10.5 11 12 13]';
> > > > for i=1:length(H);
> > > > x(1)=0;
> > > > x(i)=1+x(i-1);
> > > > y(i)=L+x(i);
> > > >
> > > > if H(j)>x(i) && H(j)<y(i);
> > > > N=H(j); %Need a matrix with H(j) as rows
> > > > end
> > > > end
> > > >
> > > > The first row of the matriy should read [1.5 2 3 4]. Any advice is appreciated.
> > >
> > > Well, the code given tries to access x(0) on line 'x(i)=1+x(i-1);' the first time through the loop. There is no 0-based indexing in Matlab (unfortunately). You also have not set a value for 'j', so the 'if...' statement will abort. I tried the code using 'for i=2:length(H);...', but I cannot determine what value you need for 'j', and so cannot help further.
> > >
> > > Anyway, there is a 'histc' command in Matlab which I think will do what you want much faster and easier. Look that up.
> >
> >
> > Thaky you. Sorry the 'j' should signify any element of matrix H which meets the criteria. Perhaps j=1:length(H);.I wasnt sure how to do this. I will check the 'histc' function
>
> Sorry for the late post, I'm reading this ML through MATLAB Newsreader which is quite laggy...

Hi the 'histc' function will not give me what I need. I need all of the values within the given range to form the rows of a single matrix. My code is a bit clearer now but still not correct. There is now a problem with the use of '&&' also. Any further suggestions?

 L=3;
        H=[1 2 3 4 5 6 7 8 9 10 11]';
        for i=1:length(H);
           x(i)=i;
           y(i)=L+x(i);
            
        j=1:length(H);
        if H(j)>x(i) && H(j)<y(i);
           N(i)=H(j)'; %Need a new matrix N with H(j) as rows
        end
        end

Subject: Splitting a long vector into matrix of variable columns

From: Mark

Date: 13 Feb, 2011 12:34:04

Message: 7 of 15

"Franck Dernoncourt" <franck.dernoncourt@gmail.com> wrote in message <ij8fsn$q1r$1@fred.mathworks.com>...
> "Mark" wrote in message <ij8f0k$p1$1@fred.mathworks.com>...
> > "Jeff" wrote in message <ij8e4f$4qk$1@fred.mathworks.com>...
> > > "Mark" wrote in message <ij8bre$9jk$1@fred.mathworks.com>...
> > > > Hello. I wish to split a very long vector into a single matrix where the rows consist of values between a certain interval. This interval is defined usinga for loop. I give a simplified example but I'm unsure of the coding. I think the problem is in the last 2 lines where I introduce the H8j).
> > > >
> > > > L=4;
> > > > H=[1 1.5 2 3 4 5.5 6 7 8 9 10.5 11 12 13]';
> > > > for i=1:length(H);
> > > > x(1)=0;
> > > > x(i)=1+x(i-1);
> > > > y(i)=L+x(i);
> > > >
> > > > if H(j)>x(i) && H(j)<y(i);
> > > > N=H(j); %Need a matrix with H(j) as rows
> > > > end
> > > > end
> > > >
> > > > The first row of the matriy should read [1.5 2 3 4]. Any advice is appreciated.
> > >
> > > Well, the code given tries to access x(0) on line 'x(i)=1+x(i-1);' the first time through the loop. There is no 0-based indexing in Matlab (unfortunately). You also have not set a value for 'j', so the 'if...' statement will abort. I tried the code using 'for i=2:length(H);...', but I cannot determine what value you need for 'j', and so cannot help further.
> > >
> > > Anyway, there is a 'histc' command in Matlab which I think will do what you want much faster and easier. Look that up.
> >
> >
> > Thaky you. Sorry the 'j' should signify any element of matrix H which meets the criteria. Perhaps j=1:length(H);.I wasnt sure how to do this. I will check the 'histc' function
>
> Sorry for the late post, I'm reading this ML through MATLAB Newsreader which is quite laggy...

Hi the 'histc' function will not give me what I need. I need all of the values within the given range to form the rows of a single matrix. My code is a bit clearer now but still not correct. There is now a problem with the use of '&&' also. Any further suggestions?

 L=3;
        H=[1 2 3 4 5 6 7 8 9 10 11]';
        for i=1:length(H);
           x(i)=i;
           y(i)=L+x(i);
            
        j=1:length(H);
        if H(j)>x(i) && H(j)<y(i);
           N(i)=H(j)'; %Need a new matrix N with H(j) as rows
        end
        end

Subject: Splitting a long vector into matrix of variable columns

From: Franck Dernoncourt

Date: 13 Feb, 2011 12:58:03

Message: 8 of 15

Filter then reshape.

%Example:
H=[1 2 3 4 5 6 7 8 9 10 11];
lower_bound = 3;
upper_bound = 10;
N = reshape(H(H > lower_bound & H < upper_bound), 2, 3);

Subject: Splitting a long vector into matrix of variable columns

From: Mark

Date: 13 Feb, 2011 13:32:03

Message: 9 of 15

"Franck Dernoncourt" <franck.dernoncourt@gmail.com> wrote in message <ij8kgr$ka0$1@fred.mathworks.com>...
> Filter then reshape.
>
> %Example:
> H=[1 2 3 4 5 6 7 8 9 10 11];
> lower_bound = 3;
> upper_bound = 10;
> N = reshape(H(H > lower_bound & H < upper_bound), 2, 3);


Thank you its not exactly what I need as the column width cannot be predetermined. I will try work some variation of this.

Subject: Splitting a long vector into matrix of variable columns

From: Bruno Luong

Date: 13 Feb, 2011 13:38:03

Message: 10 of 15

"Mark" wrote in message <ij8mgj$qu0$1@fred.mathworks.com>...
>
> Thank you its not exactly what I need as the column width cannot be predetermined. I will try work some variation of this.

The problem is nobody seems to know what you want.

Bruno

Subject: Splitting a long vector into matrix of variable columns

From: Franck Dernoncourt

Date: 13 Feb, 2011 13:51:03

Message: 11 of 15

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ij8mrr$jg0$1@fred.mathworks.com>...
> "Mark" wrote in message <ij8mgj$qu0$1@fred.mathworks.com>...
> >
> > Thank you its not exactly what I need as the column width cannot be predetermined. I will try work some variation of this.

True, you need to predetermine column width, which can be done easily by checking size(H(H > lower_bound & H < upper_bound)) depending on your needs.

Subject: Splitting a long vector into matrix of variable columns

From: Mark

Date: 3 Mar, 2011 17:27:21

Message: 12 of 15

"Franck Dernoncourt" <franck.dernoncourt@gmail.com> wrote in message <ij8nk7$8de$1@fred.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ij8mrr$jg0$1@fred.mathworks.com>...
> > "Mark" wrote in message <ij8mgj$qu0$1@fred.mathworks.com>...
> > >
> > > Thank you its not exactly what I need as the column width cannot be predetermined. I will try work some variation of this.
>
> True, you need to predetermine column width, which can be done easily by checking size(H(H > lower_bound & H < upper_bound)) depending on your needs.



I have a further extension to this problem. Here is some code to split vector X into an array of individual vectors depending on their size relative to parameter L.

L=5;
X=[1 3 4 6 7 9 11 12 13 ];
for j=1:max(X);
lower_bound = j-1;
upper_bound =L+lower_bound;
jlow = find(X>lower_bound,1,'first');
jup = find(X<upper_bound,1,'last');
a(j) = {X(jlow:jup)};
end


This gives me an array a={[1,3,4],[3,4],[3,4,6]...................}

Now suppose I have another vector of the same size , P=[100 102 110 95 120 111 115 127 132 ];

Is there a way of creating an array b which replaces the elements of array a with those of the vector P based on their relative position to the elements in vector X?

More simply an array b={[100 102 110],[110 95] [95 120 111]........}

Thanks in advance for any advice

Subject: Splitting a long vector into matrix of variable columns

From: Bruno Luong

Date: 3 Mar, 2011 18:54:05

Message: 13 of 15

"Mark" wrote in message <ikoj1p$90k$1@fred.mathworks.com>...
> "Franck Dernoncourt" <franck.dernoncourt@gmail.com> wrote in message <ij8nk7$8de$1@fred.mathworks.com>...
> > "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ij8mrr$jg0$1@fred.mathworks.com>...
> > > "Mark" wrote in message <ij8mgj$qu0$1@fred.mathworks.com>...
> > > >
> > > > Thank you its not exactly what I need as the column width cannot be predetermined. I will try work some variation of this.
> >
> > True, you need to predetermine column width, which can be done easily by checking size(H(H > lower_bound & H < upper_bound)) depending on your needs.
>
>
>
> I have a further extension to this problem. Here is some code to split vector X into an array of individual vectors depending on their size relative to parameter L.
>
> L=5;
> X=[1 3 4 6 7 9 11 12 13 ];
> for j=1:max(X);
> lower_bound = j-1;
> upper_bound =L+lower_bound;
> jlow = find(X>lower_bound,1,'first');
> jup = find(X<upper_bound,1,'last');
> a(j) = {X(jlow:jup)};
> end
>
>
> This gives me an array a={[1,3,4],[3,4],[3,4,6]...................}
>
> Now suppose I have another vector of the same size , P=[100 102 110 95 120 111 115 127 132 ];
>
> Is there a way of creating an array b which replaces the elements of array a with those of the vector P based on their relative position to the elements in vector X?
>
> More simply an array b={[100 102 110],[110 95] [95 120 111]........}
>
> Thanks in advance for any advice

I don't understand how you get b (please double check your example). In anticipation of what you want to achieve, here is a code:

X = [1 3 4 6 7 9 11 12 13 ];
P = [100 102 110 95 120 111 115 127 132];

L = 5;
lower = 0:max(X)-1;
upper = lower + L-1;

[~, ilo] = histc(lower, [-Inf X]);
[~, ihi] = histc(upper, [X Inf]);

a = arrayfun(@(lo,hi) X(lo:hi), ilo, ihi, 'Unif', false)
b = arrayfun(@(lo,hi) P(lo:hi), ilo, ihi, 'Unif', false)

Bruno

Subject: Splitting a long vector into matrix of variable columns

From: Mark

Date: 3 Mar, 2011 19:09:07

Message: 14 of 15

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ikoo4d$941$1@fred.mathworks.com>...
> "Mark" wrote in message <ikoj1p$90k$1@fred.mathworks.com>...
> > "Franck Dernoncourt" <franck.dernoncourt@gmail.com> wrote in message <ij8nk7$8de$1@fred.mathworks.com>...
> > > "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ij8mrr$jg0$1@fred.mathworks.com>...
> > > > "Mark" wrote in message <ij8mgj$qu0$1@fred.mathworks.com>...
> > > > >
> > > > > Thank you its not exactly what I need as the column width cannot be predetermined. I will try work some variation of this.
> > >
> > > True, you need to predetermine column width, which can be done easily by checking size(H(H > lower_bound & H < upper_bound)) depending on your needs.
> >
> >
> >
> > I have a further extension to this problem. Here is some code to split vector X into an array of individual vectors depending on their size relative to parameter L.
> >
> > L=5;
> > X=[1 3 4 6 7 9 11 12 13 ];
> > for j=1:max(X);
> > lower_bound = j-1;
> > upper_bound =L+lower_bound;
> > jlow = find(X>lower_bound,1,'first');
> > jup = find(X<upper_bound,1,'last');
> > a(j) = {X(jlow:jup)};
> > end
> >
> >
> > This gives me an array a={[1,3,4],[3,4],[3,4,6]...................}
> >
> > Now suppose I have another vector of the same size , P=[100 102 110 95 120 111 115 127 132 ];
> >
> > Is there a way of creating an array b which replaces the elements of array a with those of the vector P based on their relative position to the elements in vector X?
> >
> > More simply an array b={[100 102 110],[110 95] [95 120 111]........}
> >
> > Thanks in advance for any advice
>
> I don't understand how you get b (please double check your example). In anticipation of what you want to achieve, here is a code:
>
> X = [1 3 4 6 7 9 11 12 13 ];
> P = [100 102 110 95 120 111 115 127 132];
>
> L = 5;
> lower = 0:max(X)-1;
> upper = lower + L-1;
>
> [~, ilo] = histc(lower, [-Inf X]);
> [~, ihi] = histc(upper, [X Inf]);
>
> a = arrayfun(@(lo,hi) X(lo:hi), ilo, ihi, 'Unif', false)
> b = arrayfun(@(lo,hi) P(lo:hi), ilo, ihi, 'Unif', false)
>
> Bruno


Yes thats correct Bruno - it works perfect. My example wasnt so clear. Thank you.
Mark

Subject: Splitting a long vector into matrix of variable columns

From: Mark

Date: 21 Mar, 2011 11:57:04

Message: 15 of 15

"Mark" wrote in message <ikop0j$70a$1@fred.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ikoo4d$941$1@fred.mathworks.com>...
> > "Mark" wrote in message <ikoj1p$90k$1@fred.mathworks.com>...
> > > "Franck Dernoncourt" <franck.dernoncourt@gmail.com> wrote in message <ij8nk7$8de$1@fred.mathworks.com>...
> > > > "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ij8mrr$jg0$1@fred.mathworks.com>...
> > > > > "Mark" wrote in message <ij8mgj$qu0$1@fred.mathworks.com>...
> > > > > >
> > > > > > Thank you its not exactly what I need as the column width cannot be predetermined. I will try work some variation of this.
> > > >
> > > > True, you need to predetermine column width, which can be done easily by checking size(H(H > lower_bound & H < upper_bound)) depending on your needs.
> > >
> > >
> > >
> > > I have a further extension to this problem. Here is some code to split vector X into an array of individual vectors depending on their size relative to parameter L.
> > >
> > > L=5;
> > > X=[1 3 4 6 7 9 11 12 13 ];
> > > for j=1:max(X);
> > > lower_bound = j-1;
> > > upper_bound =L+lower_bound;
> > > jlow = find(X>lower_bound,1,'first');
> > > jup = find(X<upper_bound,1,'last');
> > > a(j) = {X(jlow:jup)};
> > > end
> > >
> > >
> > > This gives me an array a={[1,3,4],[3,4],[3,4,6]...................}
> > >
> > > Now suppose I have another vector of the same size , P=[100 102 110 95 120 111 115 127 132 ];
> > >
> > > Is there a way of creating an array b which replaces the elements of array a with those of the vector P based on their relative position to the elements in vector X?
> > >
> > > More simply an array b={[100 102 110],[110 95] [95 120 111]........}
> > >
> > > Thanks in advance for any advice
> >
> > I don't understand how you get b (please double check your example). In anticipation of what you want to achieve, here is a code:
> >
> > X = [1 3 4 6 7 9 11 12 13 ];
> > P = [100 102 110 95 120 111 115 127 132];
> >
> > L = 5;
> > lower = 0:max(X)-1;
> > upper = lower + L-1;
> >
> > [~, ilo] = histc(lower, [-Inf X]);
> > [~, ihi] = histc(upper, [X Inf]);
> >
> > a = arrayfun(@(lo,hi) X(lo:hi), ilo, ihi, 'Unif', false)
> > b = arrayfun(@(lo,hi) P(lo:hi), ilo, ihi, 'Unif', false)
> >
> > Bruno
>
>
>
X = [1 3 4 6 7 9 11 12 13 ];
L = 5;
lower = 0:max(X)-1;
upper = lower + L-1;
[~, ilo] = histc(lower, [-Inf X]);
[~, ihi] = histc(upper, [X Inf]);
 a=arrayfun(@(lo,hi) X(lo:hi), ilo, ihi, 'Unif', false)

The code given in the simple example above works fine but my vector is extremely large (over 4 million rows) meaning that the calculation is slightly above the available memory. Is there any alternative to reducing the X vector into smaller blocks? I tried to write to a text file using dlmwrite but without success. Perhaps he full calculation is carried out in one go using arrayfun. Any help would be appreciated.
 

Tags for this Thread

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.

rssFeed for this Thread

Contact us at files@mathworks.com