How can I compare every values in an array and put the new values in new arry

I like to compare array values of that I have. For example, I have a nxm array. I pick A = array[:,3] to compare with 0. If it is bigger than 0, the new array will be 1.
I have code as

A = [1 2 3; 4 5 6]
a = A[:,3]
@variable(m,x[1:3,1:3])
for i = 3
if a <=0 
x[:,a] ==0
else x[:,a] ==1
end
end

It shows error. I woulder how can I compare it and place it into the new array

It’s not totally clear what you want to do here. However, I am assuming you are using JuMP and trying to constrain your variables (x) using an indicator matrix (A). Your major issue is in the if statement, you can’t compare a vector (a) with a scalar value. Below is my interpretation of your code:

using JuMP
m = Model();
@variable(m,x[1:3,1:3])

A = [1 2 3; 4 5 6; 7 8 9]

colind = 3 # which column you want to constrain
for rowind=1:size(x, 1) # loop over each row
    if A[rowind, colind] <= 0 
        @constraint(m, x[rowind, colind] == 0)
    else 
        @constraint(m, x[rowind, colind] == 1)
    end
end

Sorry about that. So I have array as nxm. On the 3 column have values. What I am trying to do is, load 0 or 1 in new array.

捕获
This is what I have for now, but it should not be that much 1 in the new array.
I think what I really want to say is basic on the values in A array to replace the number in the new array. For example, in A[1:3] = 9. A[9,3] =13 and those are bigger than 0. The new array at NewA[1:9] = 1. NewA[1,13] =1. Otherwise, be 0.

I am still a little confused because you are swapping between broadcasted assignments, e.g. A[1:3] = 9 maps to NewA[1:9] = 1, and individual assignments, e.g. A[9, 3] = 13 maps to NewA[1, 13] = 1.

Let’s clarify what you mean.

  1. Suppose you have A which is 2x3 matrix. In this example, do you only care about the 3rd column, i.e. A[:, 3]?
  2. What is the size of NewA?
  3. If A[a, 3] = b (>= 0), should all the rows (at a specific column) or columns (at a specific row) or a specific index (row and column) of NewA be 1? What is the significance of a and b here?

Thank for reply so quick.
1, No, I also care about other column values. I am also use the 4th,5th same way. However, I will use the first and second in different way, not here.
2, Suppose I have a A as 100x6. The new A is 100x30.
3, if look at all values in A of 3rd column, It may like 10,3,9…(but all smaller than 30). For instance A [1,3] = 30. In new A [1,30] =1. A [6,3] =4, then new A[6,4] =1. For all other values on A[:,:] =0.

Why don’t you show us an example of an array with 5 lines and the expected result?

Does this work?

# make dummy data
nrows = 10 # can make this 100 but it displays better if its smaller
ncols = 6
A = zeros(Int64, nrows, ncols)
new_ncols = 15
A[:, 3] = rand(1:new_ncols, nrows)

# assign according to your rule
newA = zeros(Int64, nrows, new_ncols)
index_col = 3
for nrow = 1:nrows
    ncol = A[nrow, index_col]
    newA[nrow, ncol] = 1.0
end

Interesting. I tried your code. The output of the new A is what I am looking for.
I tried to put it into mymodel as

datarows = 138
datacols = 6
X = zeros(Int64, datarows, datacols)
newdatacols = 34
newX = zeros(Int64, datarows, newdatacols)
index_col = 3
for datarow = 1:datarows
    datacol = X[datarows, index_col]
    newX[datarow,datacol] =1.0
end
println(newX)

But is said the LoadError: BoundsError: attempt to access 138×34 Array{Int64,2} at index [1, 0]

data is an array that I load from csv files, and it looks like this
捕获

X is full of 0 and you are indexing into X to get datacol and then using datacol as an index, hence the error from indexing with 0.

1 Like

@pdeffebach is right, you’ll need to add an if statement to the loop to catch this input error. Something like

 for datarow = 1:datarows
    datacol = X[datarows, index_col]
    if datacol != 0
        newX[datarow,datacol] = 1.0
    end
end

Could this be what you are looking for:

A = [1 2 3; 4 5 6; 7 8 0]
a = A[:,3].>0

As I understand, he’s really doing a kind of one-hot encoding, so

index_col = 3
A = [ 2 3 1
      6 7 3
      0 3 2 ] 

gets turned into

#A[.,.]== 1 2 3... 

newA =  [ 1 0 0 
          0 0 1
          0 1 0 ] 

With the power of CartesianIndices, you can do this in a single line like this:

newA[CartesianIndex.(1:100,A[:,3])] .= 1