jeudi 1 février 2018

Create a matrix with alternate rows from two matrices [MATLAB]

I have the following question:

Write a program that starts by requesting an A matrix by keyboard. Next, if the number of columns of A is odd, the last column of additional zeros must be added. From this moment matrix, A has an even number (n) of columns. The program will divide the input matrix into two sub-matrices. The first sub-matrix (A1) contains the first n / 2 columns of A. The second sub-matrix (A2) has the last n / 2 columns. Finally, the program must calculate and write on the screen a matrix B that has the rows of A1 in the odd rows.

My attempt

A = input('Enter a matrix:')
filA = size(A,1);
colA = size(A,2);
if rem(col,2)==1
    A = [A, zeros(fil,1)]
    colA = size(A,2);
    filA = size(A,1);
    A1 = A(:, [1:colA/2])
    A2 = A(:, [1+(colA/2):colA])
    filB = size(A1,1) + size (A2,1);
    colB = colA/2;

    if rem()==1
        B = [A1(1:2:end, :); A2(2:2:end, :)]
    else
        B = A2(2:2:end, :)
    end
%B =
else
    A1 = A(:, [1:col/2])
    A2 = A(:, [1+(col/2):col])

    if rem()==1
        B = A1(1:2:end, :)
    else
        B = A2(2:2:end, :)
    end
end

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire