## see anything wrong with this piece of code?

### see anything wrong with this piece of code?

Hi again,

I put the same question before but then I didn't write everything
correctly.
The correct question comes here: I'll like to change tempmatrix if
number is equal to 1. I know that number sometimes is equal to 1 but
tempmatrix is never changing.

L=3;
numberofrows = 2^L;
numberofcols = L;
matrix = dec2bin(numberofrows-1:-1:0);
tempmatrix = zeros(numberofcols, numberofrows/2);

for j=1:numberofcols
counter = 0;
for i=1:numberofrows
counter = counter+1;
if (number == 1)
tempmatrix(j,counter)=i;
end
end
end

Are there anything wrong with my for loops or maybe the if constuction?
As I said before, tempmatrix are never changing.

### see anything wrong with this piece of code?

Peter Lindquist a crit :

> Hi again,

> I put the same question before but then I didn't write everything
> correctly.
> The correct question comes here: I'll like to change tempmatrix if
> number is equal to 1. I know that number sometimes is equal to 1 but
> tempmatrix is never changing.

> L=3;
> numberofrows = 2^L;
> numberofcols = L;
> matrix = dec2bin(numberofrows-1:-1:0);
> tempmatrix = zeros(numberofcols, numberofrows/2);

> for j=1:numberofcols
>     counter = 0;
>     for i=1:numberofrows
>         counter = counter+1;
>         if (number == 1)
>            tempmatrix(j,counter)=i;
>         end
>     end
> end

> Are there anything wrong with my for loops or maybe the if constuction?
> As I said before, tempmatrix are never changing.

from the help :
DEC2BIN Convert decimal integer to a binary string.
~~~~~~~
So what you call "number" is in fact a character.
" if (number == '1') " works

A++

David

### see anything wrong with this piece of code?

On Mon, 14 Feb 2000 13:38:09 +0100, Peter Lindquist

>Hi again,

[snip]

>for j=1:numberofcols
>    counter = 0;
>    for i=1:numberofrows
>        counter = counter+1;
>        if (number == 1)
>           tempmatrix(j,counter)=i;
>        end
>    end
>end

>Are there anything wrong with my for loops or maybe the if constuction?
>As I said before, tempmatrix are never changing.

Hi Peter,

The reason the if statement doesn't work is that your comparing a
double to a binary number, which always results in 0.

The quickest way to make this code work is to replace the if statement
with

if (number == dec2bin(1))

etc.

Though you'll probably want to put dec2bin(1) in some constant outside
the loop and use that in the comparison, which is quicker.

Joris Portegies Zwart
http://www.science.uva.nl/~portegie/

### see anything wrong with this piece of code?

On Mon, 14 Feb 2000 13:38:09 +0100, Peter Lindquist

>Hi again,

Hi yourself

Quote:>I put the same question before but then I didn't write everything
>correctly.

OK

Quote:>L=3;
>numberofrows = 2^L;
>numberofcols = L;
>matrix = dec2bin(numberofrows-1:-1:0);

Here is the problem. Try the command
whos matrix
and you'll see that matrix is not a numerial array but rather a char
matrix = dec2bin(numberofrows-1:-1:0)-48;

Quote:>tempmatrix = zeros(numberofcols, numberofrows/2);

>for j=1:numberofcols
>    counter = 0;
>    for i=1:numberofrows
>        counter = counter+1;
>        if (number == 1)
>           tempmatrix(j,counter)=i;
>        end
>    end
>end

This loop can of course be avoided altogether. Try this

L=3;
numberofrows = 2^L;
numberofcols = L;
matrix = dec2bin(numberofrows-1:-1:0)-48;
enum = repmat(1:numberofrows,numberofcols,1)
enum(~matrix') = 0

The matrix enum is equal to tempmatrix except that is has the same
size as matrix.

Lars

------------------------------

http://www.gbar.dtu.dk/~matlg

Check out my matlab toolboxes at:
http://members.xoom.com/gregersenweb/matlab/

### see anything wrong with this piece of code?

Peter,

The main problem with your code is that the function dec2bin returns a
string.  The value that gets assigned to the variable number is therefore
also a string.  So when you try to make the comparison (number == 1) in the
IF statement, it always evaluates to false because your are comparing the
string variable number to the double value 1.  You need to compare strings
with strings, i.e, use (number == '1').

That is the major error occurring.  Here are some other minor points:

1) The variable counter is unnecessary since it is always equal to i.  You
can just get rid of the lines counter = 0 and counter = counter+1, and use

2) You initialize tempmatrix to size 3x4 (because floor(numberofrow/2)==4),
but the way you have it setup it is possible to assign an element to any
column 1-9.  This won't produce an error because Matlab will automatically
adjust the matrix size, but you should really initialize your matrix to the
correct size.

The way you initialized tempmatrix makes me think that numerofrows = 2*L not
2^L.  Did you get that line correct?

3) There are much simpler ways of doing this than a for loop.  However, I am
having trouble figuring out what you really are trying to do.  Perhaps post
an example of what the input and output of your code should look like, and I
am sure some people out here in newsgroup land could help you find a more
efficient way.

Hope that helped,

Jordan

> Hi again,

> I put the same question before but then I didn't write everything
> correctly.
> The correct question comes here: I'll like to change tempmatrix if
> number is equal to 1. I know that number sometimes is equal to 1 but
> tempmatrix is never changing.

> L=3;
> numberofrows = 2^L;
> numberofcols = L;
> matrix = dec2bin(numberofrows-1:-1:0);
> tempmatrix = zeros(numberofcols, numberofrows/2);

> for j=1:numberofcols
>     counter = 0;
>     for i=1:numberofrows
>         counter = counter+1;
>         if (number == 1)
>            tempmatrix(j,counter)=i;
>         end
>     end
> end

> Are there anything wrong with my for loops or maybe the if constuction?
> As I said before, tempmatrix are never changing.

### see anything wrong with this piece of code?

:>
:> Hi again,
:>
:> I put the same question before but then I didn't write everything
:> correctly.
:> The correct question comes here: I'll like to change tempmatrix if
:> number is equal to 1. I know that number sometimes is equal to 1 but
:> tempmatrix is never changing.
:>
:> L=3;
:> numberofrows = 2^L;
:> numberofcols = L;
:> matrix = dec2bin(numberofrows-1:-1:0);
:> tempmatrix = zeros(numberofcols, numberofrows/2);
:>
:> for j=1:numberofcols
:>     counter = 0;
:>     for i=1:numberofrows
:>         counter = counter+1;
:>         if (number == 1)
:>            tempmatrix(j,counter)=i;
:>         end
:>     end
:> end
:>
:> Are there anything wrong with my for loops or maybe the if constuction?
:> As I said before, tempmatrix are never changing.
:>

:   from the help :
: DEC2BIN Convert decimal integer to a binary string.
:                                             ~~~~~~~
: So what you call "number" is in fact a character.
: " if (number == '1') " works

peter, you may want to get rid of the loops as well:

[x y] = find(matrix == '1'); temp = zeros(size(matrix)); temp(matrix == '1') = x;
temp(:,end) = []; temp = temp';

gautam.

numberofcols=3;
numberofrows=8;
tempmatrix = zeros(numberofcols, numberofrows/2);

for j=1:numberofcols
counter = 0;
for i=1:numberofrows
counter = counter+1;