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:
Generating of Data

Subject: Generating of Data

From: Lin

Date: 18 Apr, 2009 02:28:02

Message: 1 of 11

Hi all

if my input is 2;
i need something like:

0 0 0 ..... 1
0 1 1 ......1
0 0 1 ......1

and this can be generated using:
dec2bin(0:7)'-'0';

however, when i change my input; the earlier code is no longer valid. Is there a better way of doing? Some expected results are shown below.

%Expected Results
input = 3
i need something as follows:

0 0 0 0 0 0 1 1 1 1 1 1
0 0 0 1 1 1 0 0 0 1 1 1
0 1 2 0 1 2 0 1 2 0 1 2

when input is 4; it should look something like:

0 0 0 0 0 0 0 0 0 .................... 1 1 1
0 0 0 0 0 0 0 0 1 .................... 1 1 1
0 0 0 0 1 1 1 1 0 .................... 1 1 1
0 1 2 3 0 1 2 3 0 .................... 1 2 3

Thanks in advance for your help.

Subject: Generating of Data

From: Roger Stafford

Date: 18 Apr, 2009 04:44:01

Message: 2 of 11

"Lin " <shenqlv@yahoo.com> wrote in message <gsbdrh$m6u$1@fred.mathworks.com>...
> Hi all
>
> if my input is 2;
> i need something like:
>
> 0 0 0 ..... 1
> 0 1 1 ......1
> 0 0 1 ......1
>
> and this can be generated using:
> dec2bin(0:7)'-'0';
>
> however, when i change my input; the earlier code is no longer valid. Is there a better way of doing? Some expected results are shown below.
>
> %Expected Results
> input = 3
> i need something as follows:
>
> 0 0 0 0 0 0 1 1 1 1 1 1
> 0 0 0 1 1 1 0 0 0 1 1 1
> 0 1 2 0 1 2 0 1 2 0 1 2
>
> when input is 4; it should look something like:
>
> 0 0 0 0 0 0 0 0 0 .................... 1 1 1
> 0 0 0 0 0 0 0 0 1 .................... 1 1 1
> 0 0 0 0 1 1 1 1 0 .................... 1 1 1
> 0 1 2 3 0 1 2 3 0 .................... 1 2 3
>
> Thanks in advance for your help.

  If your "input" refers to the base of your digits, presumably you can make use of the matlab function 'dec2base'. I don't completely understand your examples, however.

Roger Stafford

Subject: Generating of Data

From: John D'Errico

Date: 18 Apr, 2009 09:35:02

Message: 3 of 11

"Lin " <shenqlv@yahoo.com> wrote in message <gsbdrh$m6u$1@fred.mathworks.com>...
> Hi all
>
> if my input is 2;
> i need something like:
>
> 0 0 0 ..... 1
> 0 1 1 ......1
> 0 0 1 ......1
>
> and this can be generated using:
> dec2bin(0:7)'-'0';
>
> however, when i change my input; the earlier code is no longer valid. Is there a better way of doing? Some expected results are shown below.
>
> %Expected Results
> input = 3
> i need something as follows:
>
> 0 0 0 0 0 0 1 1 1 1 1 1
> 0 0 0 1 1 1 0 0 0 1 1 1
> 0 1 2 0 1 2 0 1 2 0 1 2
>
> when input is 4; it should look something like:
>
> 0 0 0 0 0 0 0 0 0 .................... 1 1 1
> 0 0 0 0 0 0 0 0 1 .................... 1 1 1
> 0 0 0 0 1 1 1 1 0 .................... 1 1 1
> 0 1 2 3 0 1 2 3 0 .................... 1 2 3
>
> Thanks in advance for your help.

I'm a bit confused about these examples.

Worse, if this is some sort of extension of the
case for n = 2, you will run out of memory
almost immediately. When n = 3, this will
generate an array with 12*3^12 = 6377292
elements in it. If they are doubles, we are
talking 48 megabytes to store only this case.

The case for n = 4 will require about 0.5
terabytes of memory. 500 gigabytes or so,
if you prefer that unit. Does your computer
have that much ram?

So you might want to reconsider this effort,
and certainly do a better job of explaining
what you want if you want something
different.

John

Subject: Generating of Data

From: Lin Li

Date: 18 Apr, 2009 10:34:02

Message: 4 of 11

"Lin " <shenqlv@yahoo.com> wrote in message <gsbdrh$m6u$1@fred.mathworks.com>...
> Hi all
>
> if my input is 2;
> i need something like:
>
> 0 0 0 ..... 1
> 0 1 1 ......1
> 0 0 1 ......1
>
> and this can be generated using:
> dec2bin(0:7)'-'0';
>
> however, when i change my input; the earlier code is no longer valid. Is there a better way of doing? Some expected results are shown below.
>
> %Expected Results
> input = 3
> i need something as follows:
>
> 0 0 0 0 0 0 1 1 1 1 1 1
> 0 0 0 1 1 1 0 0 0 1 1 1
> 0 1 2 0 1 2 0 1 2 0 1 2
>
> when input is 4; it should look something like:
>
> 0 0 0 0 0 0 0 0 0 .................... 1 1 1
> 0 0 0 0 0 0 0 0 1 .................... 1 1 1
> 0 0 0 0 1 1 1 1 0 .................... 1 1 1
> 0 1 2 3 0 1 2 3 0 .................... 1 2 3
>
> Thanks in advance for your help.

It seems that the first 3 line act binary, while the last line acts in a [user_input -1] system, so if it is so:

in = input('input = ');
first3 = reshape(repmat((dec2bin(0:7) - '0')', in, 1), 3, []);
last = 0:in - 1;
last = repmat(last, 1, size(first3, 2)/size(last, 2));
answ = [first3; last]

Subject: Generating of Data

From: Lin Li

Date: 18 Apr, 2009 10:35:02

Message: 5 of 11

"Lin " <shenqlv@yahoo.com> wrote in message <gsbdrh$m6u$1@fred.mathworks.com>...
> Hi all
>
> if my input is 2;
> i need something like:
>
> 0 0 0 ..... 1
> 0 1 1 ......1
> 0 0 1 ......1
>
> and this can be generated using:
> dec2bin(0:7)'-'0';
>
> however, when i change my input; the earlier code is no longer valid. Is there a better way of doing? Some expected results are shown below.
>
> %Expected Results
> input = 3
> i need something as follows:
>
> 0 0 0 0 0 0 1 1 1 1 1 1
> 0 0 0 1 1 1 0 0 0 1 1 1
> 0 1 2 0 1 2 0 1 2 0 1 2
>
> when input is 4; it should look something like:
>
> 0 0 0 0 0 0 0 0 0 .................... 1 1 1
> 0 0 0 0 0 0 0 0 1 .................... 1 1 1
> 0 0 0 0 1 1 1 1 0 .................... 1 1 1
> 0 1 2 3 0 1 2 3 0 .................... 1 2 3
>
> Thanks in advance for your help.

It seems that the first 3 line act binary, while the last line acts in a [user_input -1] system, so if it is so:

in = input('input = ');
first3 = reshape(repmat((dec2bin(0:7) - '0')', in, 1), 3, []);
last = 0:in - 1;
last = repmat(last, 1, size(first3, 2)/size(last, 2));
answ = [first3; last]

Subject: Generating of Data

From: Lin

Date: 18 Apr, 2009 17:08:01

Message: 6 of 11

"Lin Li" <KuvertDEL@gmailDEL.comDEL> wrote in message <gscacm$qcr$1@fred.mathworks.com>...

> It seems that the first 3 line act binary, while the last line acts in a [user_input -1] system, so if it is so:
>
> in = input('input = ');
> first3 = reshape(repmat((dec2bin(0:7) - '0')', in, 1), 3, []);
> last = 0:in - 1;
> last = repmat(last, 1, size(first3, 2)/size(last, 2));
> answ = [first3; last]

Actually, there should be 2 prompting to the user.

1) Column (defining the height of the column)
2) input

If Column = 2 and input = 2, I need something like

0 0 1 1
0 1 0 1

if Column = 2, input = 3

0 0 0 1 1 1 2 2 2
0 1 2 0 1 2 0 1 2

if Column = 2, input = 4

0 0 0 0 1 1 1 1 2 2 2 2 3 3 3 3
0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3

if Column = 3, input = 2

0 0 0 0 1 1 1 1
0 0 1 1 0 0 1 1
0 1 0 1 0 1 0 1

if Column = 3, input = 3

0 0 0 0 0 0 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2
0 0 0 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 2 2 2
0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2

invalid when column = 1 and input = 1
invalid when column = 5 onwards, input = 5 onwards

Sorry for the misunderstanding. Thanks for helping.

Subject: Generating of Data

From: Roger Stafford

Date: 18 Apr, 2009 20:53:01

Message: 7 of 11

"Lin " <shenqlv@yahoo.com> wrote in message <gsd1dh$gij$1@fred.mathworks.com>...
> "Lin Li" <KuvertDEL@gmailDEL.comDEL> wrote in message <gscacm$qcr$1@fred.mathworks.com>...
>
> > It seems that the first 3 line act binary, while the last line acts in a [user_input -1] system, so if it is so:
> >
> > in = input('input = ');
> > first3 = reshape(repmat((dec2bin(0:7) - '0')', in, 1), 3, []);
> > last = 0:in - 1;
> > last = repmat(last, 1, size(first3, 2)/size(last, 2));
> > answ = [first3; last]
>
> Actually, there should be 2 prompting to the user.
>
> 1) Column (defining the height of the column)
> 2) input
>
> If Column = 2 and input = 2, I need something like
>
> 0 0 1 1
> 0 1 0 1
>
> if Column = 2, input = 3
>
> 0 0 0 1 1 1 2 2 2
> 0 1 2 0 1 2 0 1 2
>
> if Column = 2, input = 4
>
> 0 0 0 0 1 1 1 1 2 2 2 2 3 3 3 3
> 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3
>
> if Column = 3, input = 2
>
> 0 0 0 0 1 1 1 1
> 0 0 1 1 0 0 1 1
> 0 1 0 1 0 1 0 1
>
> if Column = 3, input = 3
>
> 0 0 0 0 0 0 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2
> 0 0 0 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 2 2 2
> 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2
>
> invalid when column = 1 and input = 1
> invalid when column = 5 onwards, input = 5 onwards
>
> Sorry for the misunderstanding. Thanks for helping.

  I still say you need 'dec2base', Lin. It would look something like:

 answ = (dec2base((0:(in^col-1)).',in,col)-'0').';

I am assuming here that for a column vector input to 'dec2base', it will output a column of strings - I don't have it on my system to try.

  Your col = 3, in = 3 example is short by six columns.

Roger Stafford

Subject: Generating of Data

From: Roger Stafford

Date: 18 Apr, 2009 23:05:04

Message: 8 of 11

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gsdejd$fpk$1@fred.mathworks.com>...
> .....
> answ = (dec2base((0:(in^col-1)).',in,col)-'0').';
> .....

  Here's an alternative method which avoids generating strings:

 % Let b = input (base) and n = no. of columns
 answ = mod(floor(((1/b).^(0:n-1).'/2)*(1:2:2*b^n)),b);

I have no idea whether it is faster, but it should at least be interesting to see why it works.

Roger Stafford

Subject: Generating of Data

From: Lin Li

Date: 19 Apr, 2009 11:11:02

Message: 9 of 11

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gsdmb0$rmb$1@fred.mathworks.com>...
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gsdejd$fpk$1@fred.mathworks.com>...
> > .....
> > answ = (dec2base((0:(in^col-1)).',in,col)-'0').';
> > .....
>
> Here's an alternative method which avoids generating strings:
>
> % Let b = input (base) and n = no. of columns
> answ = mod(floor(((1/b).^(0:n-1).'/2)*(1:2:2*b^n)),b);
>
> I have no idea whether it is faster, but it should at least be interesting to see why it works.
>
> Roger Stafford

Very interesting use of the mod and floor, great idea!
A last line should be added:
answ = flipud(answ);

Lin Li

Subject: Generating of Data

From: Lin Li

Date: 19 Apr, 2009 11:15:06

Message: 10 of 11

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gsdmb0$rmb$1@fred.mathworks.com>...
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gsdejd$fpk$1@fred.mathworks.com>...
> > .....
> > answ = (dec2base((0:(in^col-1)).',in,col)-'0').';
> > .....
>
> Here's an alternative method which avoids generating strings:
>
> % Let b = input (base) and n = no. of columns
> answ = mod(floor(((1/b).^(0:n-1).'/2)*(1:2:2*b^n)),b);
>
> I have no idea whether it is faster, but it should at least be interesting to see why it works.
>
> Roger Stafford

>> b = 4; n = 3;
>> tic; answ = (dec2base((0:(b^n-1)).',b,n)-'0').'; toc;
Elapsed time is 0.000294 seconds.
>> tic; answ = mod(floor(((1/b).^(0:n-1).'/2)*(1:2:2*b^n)),b); answ=flipud(answ); toc;
Elapsed time is 0.000147 seconds.

Subject: Generating of Data

From: Roger Stafford

Date: 19 Apr, 2009 15:54:01

Message: 11 of 11

"Lin Li" <KuvertDEL@gmailDEL.comDEL> wrote in message <gsf0s6$g2r$1@fred.mathworks.com>...
> A last line should be added:
> answ = flipud(answ);

  Yes, you're right, Lin, I got it backwards. However, you can avoid the 'flipud' step by just writing:

 answ = mod(floor(((1/b).^(n-1:-1:0).'/2)*(1:2:2*b^n)),b);

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