ND4J set values using boolean indexing and computation - java

I want to set values in an array based on an index and using a computation. In numpy I would write the following:
array[array < 0] = 2 * array[array < 0]
Can something like this be achieved with ND4J as well?
Edit: And what about more complicated statements / indexing such as:
array[array2 < 0] = 2 * array3[array2 < 0]
(Assuming dimensions of array1, array2 and array3 match)

Conditional assign is what you would be the closest:
import org.nd4j.linalg.indexing.BooleanIndexing;
import org.nd4j.linalg.indexing.conditions.Conditions;
INDArray array1 = Nd4j.create(new double[] {1, 2, 3, 4, 5, 6, 7});
INDArray array2 = Nd4j.create(new double[] {7, 6, 5, 4, 3, 2, 1});
INDArray comp = Nd4j.create(new double[] {1, 2, 3, 4, 3, 2, 1});
BooleanIndexing.replaceWhere(array1, array2, Conditions.greaterThan(4));
There are quite a few conditions and things you can use in there. I would suggest using the second array and applying your arithmetic you want on a whole array and letting BooleanIndexing replace what you want selecting elements from the second array you pass in.

Related

How to count a no in array without loop?

Let's assume we have an array of 10 elements a[] = {2, 3, 4, 5, 3, 2, 3, 3, 3, 2}; and if have to check the number of occurrences of an specific element say 3 . Then how I will able to count it without loop? Because the number of elements may be so many. My question is: is there any Java method to find it?
Without writing a traditional for loop:
List<Integer> list = Arrays.asList(2, 3, 4, 5, 3, 2, 3, 3, 3, 2);
long occurences = list.stream().filter(n -> n == 3).count();
System.out.print(occurences);
With an array which is an object where you you have to iterate over elements to know each one, a loop is unavoidable.
My question is: is there any Java method to find it?
Yes but you should use a structure more suitable to your need such as a Map for example.
The idea is to use a Map to initialize values and the associated frequency. In this way, you didn't need any longer to perform loop when you want to know the frequency of a integer value.
public Map<Integer,Integer> createWithElements(int... values) {
Map<Integer,Integer> nbOccurencesByValue = new HashMap<>();
for (int value : values){
Integer actualNbOccurence = nbOccurencesByValue.get(value);
if (actualNbOccurence==null){
actualNbOccurence=0;
}
nbOccurencesByValue.put(value, ++actualNbOccurence);
}
return nbOccurencesByValue;
}
How to use it :
Map<Integer,Integer> nbOccurencesByValue = createWithElements(2, 3, 4, 5, 3, 2, 3, 3, 3, 2);
Integer nbOccurenceFor3Number = nbOccurencesByValue.get(3);

Multidimensional array in Scala

Is it possible to create a multidimensional array in scala without using the "Array()"
Like this in java:
int[][] myIntArray = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
If i understood correctly, you dont want to declare the array repeating Array a lot of times.
You can try this:
val > = Array
val x: Array[Array[Int]] = >(
>(1, 2, 3),
>(4, 5, 6),
>(7, 8, 9)
)
Source (There are other suggestions also)

Inline Array Definition in Java

Sometimes I wish I could do this in Java:
for (int i : {1, 2, 3, 4, 5})
System.out.println(i);
Unfortunately, I have to do something like this instead:
int [] i = {1, 2, 3, 4, 5};
// ...
My recollection is that C++ has this sort of feature. Is there an OOP replacement for inline array definitions (maybe even to the point of instantiating anonymous classes)?
I think the closest you are gonna get is:
for(int i : new int[] {1,2,3,4})
You could create the int[] array in the for loop.
for (int i : new int[] {1, 2, 3, 4, 5}) {
...
}
Here you are making an anonymous int array, which is the closest thing to what you want. You could also loop through a Collection.
Note that this question has nothing to do with OOP. It's merely a matter of syntax. Java supports anonymous arrays/objects just like C++.
You don't have to create a separate variable for this. The syntax {1, 2, ...} is valid only for declarations, but you can always say new int[] {1, 2, ...}:
for (int i : new int[] {1, 2, 3, 4, 5})

How to add a 1D array to a 2D array?

Sorry first time asking a question here.
If I have a 2D Array like this:
int[][] array2d = {{1, 2, 3}, {6, 7, 8}};
How do I add multiple 1D Arrays like this:
int[] array1d = {3, 2, 1};
int[] array1d2 = {8, 7, 6};
so that my original 2d array becomes this:
int[][] array2d = {{1, 2, 3}, {6, 7, 8}, {3, 2, 1}, {8, 7, 6}};
Note: this is for adding information from a JTextfield into a JTable whenever a button is pressed. So, the 2d array will be used as the data inside the table. If there is a better way to accomplish this I would appreciate it too. =)
Your array :
int[][] array2d = {{1, 2, 3}, {6, 7, 8}};
is fixed in size, so you would have to create a copy with enough capacity to hold the new values:
int[][] newArray = Arrays.copyOf(array2d, 4);
newArray[2] = array1d;
newArray[3] = array1d2;
To add your data to the JTable the arrays would have to be first converted to a non-primitive type such as an Integer array. One option is to use the Apache Commons:
model.addRow(ArrayUtils.toObject(array));
for each row of the array.
arrays are fixed size so to append it you need to resize the array look at java.util.Arrays.
then set the arrays location
arra2d[index] = array1d;
is there are reason you are not using
TableModel.addRow(dataArray);
?

How to use java.util.Arrays

I'm trying to use the java.util.Arrays class in JavaSE 6 but not sure how i would implement it? on an array that I have generated?
before the start of the class i have
import java.util.Arrays
Java Arrays
To declare an array of integers, you start with:
int[] myArray;
To instantiate an array of ten integers, you can try:
myArray = new int[10];
To set values in that array, try:
myArray[0] = 1; // arrays indices are 0 based in Java
Or at instantiation:
int[] myArray2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
To get values from the array, try:
System.out.println(myArray[0]);
To print all the values in an array, try:
// go from 0 to one less than the array length, based on 0 indexing
for(int i = 0; i < myArray2.length; i++) {
System.out.println(myArray2[i]);
}
For more information, the tutorial from Sun/Oracle will be of great help. You can also check out the Java language specification on Arrays.
Using the Arrays Utility Class
java.util.Arrays contains a bunch of static methods. Static methods belong to the class and do not require an instance of the class in order to be called. Instead they are called with the class name as a prefix.
So you can do things like the following:
// print a string representation of an array
int[] myArray = {1, 2, 3, 4};
System.out.println(Arrays.toString(myArray));
Or
// sort a list
int[] unsorted = {3, 4, 1, 2, 5, 7, 6};
Arrays.sort(unsorted);
Well let's say you have an array
int[] myArray = new int[] { 3, 4, 6, 8, 2, 1, 9};
And you want to sort it. You do this:
// assumes you imported at the top
Arrays.sort(myArray);
Here's the whole shebang:
import java.util.Arrays;
class ArrayTest {
public static void main(String[] args) {
int[] myArray = new int[] { 3, 4, 6, 8, 2, 1, 9};
Arrays.sort(myArray);
System.out.println(Arrays.toString(myArray));
}
}
And that results in
C:\Documents and Settings\glow\My Documents>java ArrayTest
[1, 2, 3, 4, 6, 8, 9]
C:\Documents and Settings\glow\My Documents>
You have not provided enough information about what you are trying to do. java.util.Arrays only exposes static methods, so you simply pass in your array and whatever other params are necessary for the particular method you are calling. For instance Arrays.fill(myarray,true) would fill a boolean array with the value true.
Here is the javadoc for java.util.Arrays
You can use a static import
import static java.util.Arrays.*;
int[] ints = {3, 4, 1, 2, 5, 7, 6};
sort(ints);
public static void main(String[] args) {
double array[] = {1.1,2.3,5.6,7.5, 12.2, 44.7,4.25, 2.12};
Arrays.sort(array,1,3);
for(int i =0;i <array.length;i++){
System.out.println(array[i]);
}
}
result:
"1.1,2.3,5.6,7.5,12.2,44.7,4.25,2.12"

Categories