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

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

how to reverse a matrix from bottom to top row wise and then pick out odd rows?

Asked by Malik on 24 Apr 2013

hello

if i have this matrix A =

        10000
        00010
        10101
        10011
        10000
        00001
the rows can be from 1 to 2.^m-2 where m lets say 5. so that gives us 30 rows. i want to flip these rows from bottom to top so that the last row is the first, the second last becomes the second, the third last the third.

flipped A =

      00001
      10000
      10011
      10101
      00010
      10000

then i want to pick the odd numbered rows from the flipped order (the first, third, fifth) such that 1:2:2t rows are picked out and displayed as shown below (here t = 3)

         00001
         10011
         00010

can anyone help out with this?

regards

1 Comment

Jan Simon on 24 Apr 2013

What doe "matrix A = 10000 ..." exactly mean? Is this a CHAR matrix /then you forgot the quotes), or a double matrix (then the leading zeros are meaningless). It is not clear, what "the rows can be from 1 to 2.^m-2" means. Do you mean the size of the matrix or the decimal representatioon of the values of the elements or rows?

Malik

Products

No products are associated with this question.

1 Answer

Answer by Andrei Bobrov on 24 Apr 2013
Edited by Andrei Bobrov on 24 Apr 2013
Accepted answer
A =[10000
        00010
        10101
        10011
        10000
        00001];
A = num2str(A);
b = strrep(A(:)',' ','0');
A(:) = b; % A is string
out1 = flipud(A);
out2 = out1(1:2:end,:);

ADD after the Malik's comment

A = [ 1  1  0  0  0  0
        1  0  1  0  0  1
        1  0  1  0  0  1
        1  0  1  1  1  1
        1  0  1  0  0  1
        1  1  1  0  1  1
        1  0  1  1  1  1
        1  1  1  1  0  1
        1  0  1  0  0  1
        1  1  1  0  1  1
        1  1  1  0  1  1
        1  1  0  1  1  1
        1  0  1  1  1  1
        1  1  0  1  1  1
        1  1  1  1  0  1
        1  0  0  1  0  1
        1  0  1  0  0  1
        1  0  1  1  1  1
        1  1  1  0  1  1
        1  1  1  1  0  1
        1  1  1  0  1  1
        1  1  0  1  1  1
        1  1  0  1  1  1
        1  0  0  1  0  1
        1  0  1  1  1  1
        1  1  1  1  0  1
        1  1  0  1  1  1
        1  0  0  1  0  1
        1  1  1  1  0  1
        1  0  0  1  0  1
        1  0  0  1  0  1];
out1 = flipud(A);
out2 = out1(1:2:end,:);

6 Comments

Malik on 24 Apr 2013

yes it works! Thanks!

Malik on 24 Apr 2013

i have another question and since it is linked to this i find it convenient to post it right here, if i need to take the lcm of the binary rows that have come ut finally in out2 i am doing this:

converting each row to dec using num2str(bin2dec(out2(:))) (but doesnt work )

then applying lcm (out3)

% but the lcm function wont work for more than two numbers which is pretty challenging here, how to work through this?

Malik on 24 Apr 2013

i have converted out3 = num2str(out2(:)) to string this way.

Now to convert out3 to decimal i need to pick the string 5 at a time and convert to dec: bin2dec(out3(1:end)) then take the lcm of all the integers.

Andrei Bobrov

Contact us