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:
Space one array and other questions

Subject: Space one array and other questions

From: fckool

Date: 10 Jun, 2010 15:52:23

Message: 1 of 14

Hi,

i will use this topic to some question about the work I'm doing. (encoding code words of block linear)

1st I have this code to generate an array with all possibility of binary combinations:

for i=1:2^k
m(1,1:k)=dec2bin(0,k);
m(i,1:k)=dec2bin(i-1,k);
end


I want one space between each bit... how can i do it?

Thank you in advance

Subject: Space one array and other questions

From: fckool

Date: 10 Jun, 2010 18:02:58

Message: 2 of 14

it's urgent

Nobody knows?

Subject: Space one array and other questions

From: dpb

Date: 10 Jun, 2010 18:24:45

Message: 3 of 14

fckool wrote:
> it's urgent

Best be getting busy then, I reckon...

> Nobody knows?

Only that I know I don't understand the question.

Give an example of what you mean by a space between each bit...if you
mean you want character representation, something like

sprintf('%c %c %c \n',m')

might suit. If you have something else in mind, I'll have to wait until
the crystal comes back from the shop (again)...

--

Subject: Space one array and other questions

From: Walter Roberson

Date: 10 Jun, 2010 18:26:04

Message: 4 of 14

fckool wrote:

> 1st I have this code to generate an array with all possibility of binary combinations:
>
> for i=1:2^k
> m(1,1:k)=dec2bin(0,k);
> m(i,1:k)=dec2bin(i-1,k);
> end

> I want one space between each bit... how can i do it?

Start by fixing your code. Every iteration of the array, you write over
m(1,1:k) with '0', which is not necessary because when "i" is 1, you store
that same value in the same location.

 > It's urgent

m = repmat(' ', 2^k, 2*k-1);
for i = 1 : 2^k
m(i,1:2:2*k-1) = dec2bin(i-1,k);
end

Subject: Space one array and other questions

From: fckool

Date: 10 Jun, 2010 18:51:03

Message: 5 of 14

what is the difference between that code and this one?:

for i = 1:2^k
        for j = k:-1:1
            if rem(i-1,2^(-j+k+1))>=2^(-j+k)
                m1(i,j)=1;
            else
                m1(i,j)=0;
            end
        end
   end

after this I want do this:

c=mod(m*G,2)
´
but it give me an error:
??? Error using ==> mtimes
Inner matrix dimensions must agree.

G it's an array calculated before this.

Subject: Space one array and other questions

From: Walter Roberson

Date: 10 Jun, 2010 19:03:11

Message: 6 of 14

fckool wrote:

> c=mod(m*G,2)
> ´
> but it give me an error:
> ??? Error using ==> mtimes
> Inner matrix dimensions must agree.
>
> G it's an array calculated before this.


* is the Matrix Multiplication operator. As per usual, m*G is only possible if
the second dimension of m is the same as the first dimension of G. If you want
to multiply each corresponding element of m by each corresponding element of
G, then use m .* G .

By the way, watch out for m1 versas m .

Subject: Space one array and other questions

From: dpb

Date: 10 Jun, 2010 19:05:03

Message: 7 of 14

fckool wrote:
...

> c=mod(m*G,2)
> ´
> but it give me an error:
> ??? Error using ==> mtimes
> Inner matrix dimensions must agree.
>
> G it's an array calculated before this.

Well, "*" is matrix multiplication in Matlab so one must have conformant
array sizes for the operation as defined by matrix operations.

If G is same dimensions as m and you mean element-wise mulitplication,
that's ".*" in Matlab

doc times
doc mtimes

--

Subject: Space one array and other questions

From: fckool

Date: 10 Jun, 2010 21:28:33

Message: 8 of 14

with the code that I showed you, it works... but I cant use it.. I have to use another one..

Your code maybe not work because when you add a space (string value?) you are increasing the array dim, not?

who can I separate the bits, for a better read, without increase its size?

Subject: Space one array and other questions

From: Walter Roberson

Date: 10 Jun, 2010 21:48:14

Message: 9 of 14

fckool wrote:

> Your code maybe not work because when you add a space (string value?) you are increasing the array dim, not?

It does increase the dimensions, but that isn't necessarily a problem.

> who can I separate the bits, for a better read, without increase its size?

m = repmat(' ', 2^k, k);
for i = 1 : 2^k
m(i,:) = dec2bin(i-1,k);
end

mdisplay = repmat(' ', 2^k, 2*k-1);
mdisplay(:,1:2:2*k-1) = m;


Or, if you prefer an expression over efficiency,

mdisplay = cell2mat(arrayfun(@(c) [c ' '], m, 'Uniform',0))

Subject: Space one array and other questions

From: fckool

Date: 10 Jun, 2010 22:58:12

Message: 10 of 14

perfect, know it works.

I have to explain every line of my program can you to explain this 2 lines please?:

mdisplay = repmat(' ', 2^k, 2*k-1);
mdisplay(:,1:2:2*k-1) = m;

The efficient expression it's very good but I never gonna get it explains by myself lol..


I have another question:

how can I test the efficiency of my code? (how much time it need to run)


thank you very much, and i apology if I'm being boring.

Subject: Space one array and other questions

From: Oleg Komarov

Date: 11 Jun, 2010 07:34:06

Message: 11 of 14

fckool <xtrangekid@sapo.pt> wrote in message <1706344931.315227.1276210722145.JavaMail.root@gallium.mathforum.org>...
> perfect, know it works.
>
> I have to explain every line of my program can you to explain this 2 lines please?:
>
> mdisplay = repmat(' ', 2^k, 2*k-1);
> mdisplay(:,1:2:2*k-1) = m;
>
> The efficient expression it's very good but I never gonna get it explains by myself lol..
>
>
> I have another question:
>
> how can I test the efficiency of my code? (how much time it need to run)
>
>
> thank you very much, and i apology if I'm being boring.

To understand the code you can highlight in the m-file the sigle parts and execute it. Another thing you can do is to place the cursor on the function and press F1 or type in the command propmt "doc repmat".

Example:
repmat(' ', 2^k, 2*k-1);
Start by higlighting "2*k-1" execute it...If k = 3 then the expression evaluates to 3.

then you can mentally rewrite the expression as:
repmat(' ',4,3) --> repeat the ' ' (blank) for 4 rows and 3 columns.

To measure the performance of your code you can read in the help about the profiler.

Oleg

Subject: Space one array and other questions

From: Jos (10584)

Date: 11 Jun, 2010 09:52:06

Message: 12 of 14

Walter Roberson <roberson@hushmail.com> wrote in message <hurmm4$32t$1@canopus.cc.umanitoba.ca>...
> fckool wrote:
>
> > Your code maybe not work because when you add a space (string value?) you are increasing the array dim, not?
>
> It does increase the dimensions, but that isn't necessarily a problem.
>
> > who can I separate the bits, for a better read, without increase its size?
>
> m = repmat(' ', 2^k, k);
> for i = 1 : 2^k
> m(i,:) = dec2bin(i-1,k);
> end

DEC2BIN is vectorized, so you can skip the for-loop:

k = 3 ;
m = dec2bin(0:2^k-1,k)

Jos

Subject: Space one array and other questions

From: fckool

Date: 11 Jun, 2010 13:01:09

Message: 13 of 14

thank you for teach me to use the help.

It was more easy try explain it than try to teach me how to use the help.

I already tried to use it..


If everybody could know everything with the help, the forum would not make sense, but ok thank you anyway.

Subject: Space one array and other questions

From: fckool

Date: 11 Jun, 2010 13:03:57

Message: 14 of 14

you are right, but in that way I cant have a space between each value in the array.

Thank you for the advice

Tags for this Thread

No tags are associated with 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