1

I have an operation to make in java which I cannot find a way around without doing a for loop which I really want to avoid for computation efficiency. I used to program in Matlab and its pretty easy to do it, but in java seems a lot more tricky. My question is this... What would be the java equivalent of this Matlab code:

A = [1;-3;2;2;5-7;0];

A<1;

ans =

     0
     1
     0
     0
     1
     1

in java I tried this method while roaming the internet.

Integer[] array = {-1,2,-3,4,-5,-6};

List<Integer> result = Arrays.stream(array).filter(w ->  w < 1 )
                .collect(Collectors.toList());

In this example result = {-1,-3,-5,-6} and but I wish to have result = {1,0,1,0,1,1}

m7913d
  • 10,244
  • 7
  • 28
  • 56
kingTy
  • 13
  • 3
  • A for loop seems appropriate here. Note that using a for loop in matlab is much worse (slower) than using it in java. – m7913d Sep 07 '17 at 12:53
  • 1
    @TylerNichols For loops are relative slow in matlab due to its interpreted nature. When you write `A < 1`, this _for loop_ is evaluated using an efficient C implementation, which definitely matters for large matrices. – m7913d Sep 07 '17 at 12:57
  • The simple reason is that in reality I will have a vector with thousands values, and I cannot see myself let the program going on a loop just to get another vector of the same size. – kingTy Sep 07 '17 at 12:59
  • @m7913d thanks for clarification I did not know that optimization occured -- still a for loop somewhere even if it goes all the way down to assembly :) (unrolling aside.....) – Tyler Nichols Sep 07 '17 at 13:03

4 Answers4

3
    int[] a = {1,-3,2,2,-2,0};
    Arrays.stream(a).forEach(i -> System.out.println(i < 1 ? 1 : 0));
Mehdi Javan
  • 1,081
  • 6
  • 25
  • 1
    just to note I would benchmark this vs a for loop - I bet the results will surprise you :) see https://stackoverflow.com/questions/41278712/when-should-streams-be-preferred-over-traditional-loops-for-best-performance-do – Tyler Nichols Sep 07 '17 at 13:06
  • 1
    @TylerNichols Although I knew it might be slower, I surprised because I didn't expect such difference. An upvote for your post and comment :) – Mehdi Javan Sep 07 '17 at 13:10
2

Old school solution (assuming that X is some int value; and that `intValues' represents some sort of collection/array of ints or Integers):

List<Boolean> lessThanX = new ArrayList<>();
for (int i : intValues) {
  lessThanX.add( i < X );
}

Nowadays, using streams:

intValues.stream().map( i -> i < X ).collect(Collectors.asList());

( or something alike ... as the question contains zero effort of trying yourself - I omitted cross-checking my input - it is merely meant as inspiration to get you going )

GhostCat
  • 137,827
  • 25
  • 176
  • 248
1

This does look like it should be a simple loop function.

double[] outputArray = {1, -3, 2, 2, 5, -7, 0};
boolean[] outputArray = new boolean[inputArray.length];
for (int i = 0; i < inputArray.length; i++) {
    outputArray[i] = inputArray[i]<1;
}

This can easily be changed into a method call with the outputArray as an argument, which will probably be most suited to your needs.

MartinByers
  • 1,240
  • 1
  • 9
  • 15
0
public static int[] convertToBinary(int[] decimal, int compareInt){
  int[] binary = new int[decimal.length];
  int index = 0;
  Arrays.stream(decimal).forEach( dec -> binary[index++] = dec < compareInt ? 1 : 0 );
  return binary;
}
Sahil Chhabra
  • 10,621
  • 4
  • 63
  • 62