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
tempmatrix(j,i) instead.
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;
> rad = matrix(i,:);
> number = rad(j);
> 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.