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.
Suppose you have A which is 2x3 matrix. In this example, do you only care about the 3rd column, i.e. A[:, 3]?
What is the size of NewA?
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.
# 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