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

**URL:** https://discourse.julialang.org/t/how-can-i-compare-every-values-in-an-array-and-put-the-new-values-in-new-arry/51319
**Category:** New to Julia
**Tags:** question
**Created:** [December 5, 2020, 8:54pm UTC](https://discourse.julialang.org/t/how-can-i-compare-every-values-in-an-array-and-put-the-new-values-in-new-arry/51319 "2020-12-05T20:54:16Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![sui](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sui/32/19863_2.png) [@sui](https://discourse.julialang.org/u/sui)
#### Post date: [December 5, 2020, 8:54pm UTC](https://discourse.julialang.org/t/how-can-i-compare-every-values-in-an-array-and-put-the-new-values-in-new-arry/51319/1 "2020-12-05T20:54:16Z")

</div>

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

```julia
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

---

<div class="post-metadata">

### Author: ![Elmo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elmo/32/17979_2.png) [@Elmo](https://discourse.julialang.org/u/Elmo)
#### Post date: [December 5, 2020, 9:25pm UTC](https://discourse.julialang.org/t/how-can-i-compare-every-values-in-an-array-and-put-the-new-values-in-new-arry/51319/2 "2020-12-05T21:25:00Z")

</div>

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:

```julia
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

```

---

<div class="post-metadata">

### Author: ![sui](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sui/32/19863_2.png) [@sui](https://discourse.julialang.org/u/sui)
#### Post date: [December 5, 2020, 9:36pm UTC](https://discourse.julialang.org/t/how-can-i-compare-every-values-in-an-array-and-put-the-new-values-in-new-arry/51319/3 "2020-12-05T21:36:07Z")

</div>

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.

![捕获](https://global.discourse-cdn.com/julialang/original/3X/c/4/c4d185d327f1a8eddef7aef77004b834797f2086.png)  
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.

---

<div class="post-metadata">

### Author: ![Elmo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elmo/32/17979_2.png) [@Elmo](https://discourse.julialang.org/u/Elmo)
#### Post date: [December 5, 2020, 10:03pm UTC](https://discourse.julialang.org/t/how-can-i-compare-every-values-in-an-array-and-put-the-new-values-in-new-arry/51319/4 "2020-12-05T22:03:14Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![sui](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sui/32/19863_2.png) [@sui](https://discourse.julialang.org/u/sui)
#### Post date: [December 5, 2020, 10:12pm UTC](https://discourse.julialang.org/t/how-can-i-compare-every-values-in-an-array-and-put-the-new-values-in-new-arry/51319/5 "2020-12-05T22:12:19Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [December 5, 2020, 10:24pm UTC](https://discourse.julialang.org/t/how-can-i-compare-every-values-in-an-array-and-put-the-new-values-in-new-arry/51319/6 "2020-12-05T22:24:34Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Elmo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elmo/32/17979_2.png) [@Elmo](https://discourse.julialang.org/u/Elmo)
#### Post date: [December 5, 2020, 10:28pm UTC](https://discourse.julialang.org/t/how-can-i-compare-every-values-in-an-array-and-put-the-new-values-in-new-arry/51319/7 "2020-12-05T22:28:36Z")

</div>

Does this work?

```julia
# 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

```

---

<div class="post-metadata">

### Author: ![sui](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sui/32/19863_2.png) [@sui](https://discourse.julialang.org/u/sui)
#### Post date: [December 6, 2020, 12:01am UTC](https://discourse.julialang.org/t/how-can-i-compare-every-values-in-an-array-and-put-the-new-values-in-new-arry/51319/8 "2020-12-06T00:01:42Z")

</div>

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

```julia
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  
 ![捕获](https://global.discourse-cdn.com/julialang/original/3X/9/9/99eec23d76b09f11da00cac1398ccf5fb39fa52b.png)

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [December 6, 2020, 1:26am UTC](https://discourse.julialang.org/t/how-can-i-compare-every-values-in-an-array-and-put-the-new-values-in-new-arry/51319/9 "2020-12-06T01:26:17Z")

</div>

`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`.

---

<div class="post-metadata">

### Author: ![Elmo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elmo/32/17979_2.png) [@Elmo](https://discourse.julialang.org/u/Elmo)
#### Post date: [December 6, 2020, 11:02am UTC](https://discourse.julialang.org/t/how-can-i-compare-every-values-in-an-array-and-put-the-new-values-in-new-arry/51319/10 "2020-12-06T11:02:18Z")

</div>

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

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

```

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [December 6, 2020, 12:26pm UTC](https://discourse.julialang.org/t/how-can-i-compare-every-values-in-an-array-and-put-the-new-values-in-new-arry/51319/11 "2020-12-06T12:26:00Z")

</div>

Could this be what you are looking for:

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

```

---

<div class="post-metadata">

### Author: ![FPGro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fpgro/32/20822_2.png) [@FPGro](https://discourse.julialang.org/u/FPGro)
#### Post date: [December 7, 2020, 7:13am UTC](https://discourse.julialang.org/t/how-can-i-compare-every-values-in-an-array-and-put-the-new-values-in-new-arry/51319/12 "2020-12-07T07:13:26Z")

</div>

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

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

```

gets turned into

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

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

```

---

<div class="post-metadata">

### Author: ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)
#### Post date: [December 7, 2020, 2:56pm UTC](https://discourse.julialang.org/t/how-can-i-compare-every-values-in-an-array-and-put-the-new-values-in-new-arry/51319/13 "2020-12-07T14:56:10Z")

</div>

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

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

```
