Matrix assignment

I am trying to run a while loop which will give me a new Kx1 vector each time. Suppose the vector is stored in variable a.I'm trying to create a matrix which will have this vector as column entities, the number of columns being equal to the number of loops.

fi={};

do while col<1000;
    ........

    do while row<50;
        fi[row,col]= a[row]; 
        row=row+1;
    endo;

    col=col+1;
endo;

The fi matrix has only elements in the first column(that from the execution of final loop values).How can I get a matrix with columns containing the Kx1 vectors?

1 Answer



0



accepted

Here is a simple example program that will create a new vector a on each loop iteration and assign it to a column of fi. Each a is just a vector of integers all equal to the loop iteration number to make the output easy to understand

nrows = 10;
ncols = 4;

//Pre-allocate size of final matrix
fi = zeros(nrows, ncols);

//Loop over each column
for i(1, ncols, 1);

    //Get next 'a' vector
    a = getvec(nrows, i);

    //Assign the contents of 'a'
    //to the ith column of 'fi'
    fi[.,i] = a;
endfor;

//Return a vector of integers
//to simulate a process of creating a vector
proc (1) = getvec(nrows, i);
    retp(ones(nrows,1) .* i);
endp;

aptech

1,773

Your Answer

1 Answer

0
accepted

Here is a simple example program that will create a new vector a on each loop iteration and assign it to a column of fi. Each a is just a vector of integers all equal to the loop iteration number to make the output easy to understand

nrows = 10;
ncols = 4;

//Pre-allocate size of final matrix
fi = zeros(nrows, ncols);

//Loop over each column
for i(1, ncols, 1);

    //Get next 'a' vector
    a = getvec(nrows, i);

    //Assign the contents of 'a'
    //to the ith column of 'fi'
    fi[.,i] = a;
endfor;

//Return a vector of integers
//to simulate a process of creating a vector
proc (1) = getvec(nrows, i);
    retp(ones(nrows,1) .* i);
endp;


You must login to post answers.

Have a Specific Question?

Get a real answer from a real person

Need Support?

Get help from our friendly experts.