1
votes

Assume I have one matrix with two columns. Every column has thousands of values. The elements of the 1st column are normal numbers. The elements of the 2nd column are 0 or 1. For simplicity, take this example:

U = [ 25 36 12 52 32 12 14 ; 0 1 1 0 0 0 1]'

I want to create a new one column matrix P such that if the element of the 2nd column of U is 0, then the element of P is the same as the corresponding one in the 1st column. But, if the element of the 2nd column of U is 1, then the element of P is zero.

So, the result of the above example is:

P = [25 0 0 52 32 12 0]'
3

3 Answers

4
votes

That would be then:

> P= U(:, 1).* ~U(:, 2)
P =
   25
    0
    0
   52
   32
   12
    0
2
votes

you didn't tell which programming language this answer is in c#

void process()
{
    int column_length = 10;
    int columns = 3;
    int[,] k = new int[column_length, columns];

    /*fill array
        .
        .
        .*/
    //process it
    for (int i = 0; i < k.GetLength(0); i++)
    {
        if (k[i, 1] == 1)//if this row column 0 =1
        {
            k[i, 2] = 0;
        }
        else if (k[i, 1] == 0)//if this row column 0 =0
        {
            k[i, 2] = k[i, 0];
        }
    }
}
0
votes

With numpy, in addition to eat's answer, you could use

numpy.where(U[:,1], 0, U[:,0])