How to Round All Values in a Matrix

How to round all values in a matrix?

Just as in your other question, use the function (round) you found :)

corrs <- round(cor(dataset, use="pairwise.complete.obs"), 2)

For example:

> round(cor(cars),2)
speed dist
speed 1.00 0.81
dist 0.81 1.00

How to round a numpy array?

Numpy provides two identical methods to do this. Either use

np.round(data, 2)

or

np.around(data, 2)

as they are equivalent.

See the documentation for more information.


Examples:

>>> import numpy as np
>>> a = np.array([0.015, 0.235, 0.112])
>>> np.round(a, 2)
array([0.02, 0.24, 0.11])
>>> np.around(a, 2)
array([0.02, 0.24, 0.11])
>>> np.round(a, 1)
array([0. , 0.2, 0.1])

Java: How to round up all of the elements in an array to two digits after decimal point?

Simply use Math.round()

double[] array = new double[]{12.5698, 123.12345,0.1, 41234.212431324};    
for (int i=0;i<array.length;i++) {
array[i] = Math.round(array[i] * 100.0) / 100.0;
System.out.println(array[i]);
}

in order to print out the whole array full of float values, you
can't just loop (i) times and do System.out.println("%.2f", array[i]);

You can but using System.out.format()

for(int i=0;i<array.length;i++)
{
System.out.format("%.2f ",array[i]);

}

Output: 12.57 123.12 0.10 41234.21

How to round all the values in an array to 2 decimal points

Loops!

var x = 0;
var len = my_array.length
while(x < len){
my_array[x] = my_array[x].toFixed(2);
x++
}

And, yes, a while loop is faster here.

Rounding decimal values to nearest integers in a matrix

IIUC, you can use np.where:

m = np.linspace(1, 10, 13)
a = np.where(m - m.astype(int) >= 0.5, np.ceil(m), np.floor(m))

Output:

>>> m
array([ 1. , 1.75, 2.5 , 3.25, 4. , 4.75, 5.5 , 6.25, 7. ,
7.75, 8.5 , 9.25, 10. ])

>>> a
array([ 1., 2., 3., 3., 4., 5., 6., 6., 7., 8., 9., 9., 10.])

How to round values in R list - round only numeric values?

You have a list. Don't use apply, that will coerce to a matrix, instead use lapply in conjunction with [<- to replace the contents of myData while keeping the original structure,

Now, I can't see any reason why you would be attempting to coerce to a character when you could just use round and retain a numeric field

myData[] <- lapply(myData,round,2)
myData
# col1 col2 col3 col4
# Measure 0.96 0.96 0.96 0.87
# c-value 0.02 0.02 0.05 0

How do round off the elements of scipy.sparse.csr matrix to 2 decimal places ?

We can apply np.round to the data attribute of the matrix:

In [34]: from scipy import sparse
In [35]: M = sparse.random(5,5,.2,'csr')
In [36]: M
Out[36]:
<5x5 sparse matrix of type '<class 'numpy.float64'>'
with 5 stored elements in Compressed Sparse Row format>
In [37]: M.A
Out[37]:
array([[0. , 0. , 0.28058287, 0. , 0. ],
[0. , 0. , 0. , 0. , 0. ],
[0. , 0.81478819, 0. , 0. , 0. ],
[0. , 0. , 0. , 0.06805299, 0.51048128],
[0. , 0. , 0. , 0.64388578, 0. ]])
In [38]: M.data
Out[38]: array([0.28058287, 0.81478819, 0.06805299, 0.51048128, 0.64388578])

In [39]: M.data=np.round(M.data,2)
In [40]: M.data
Out[40]: array([0.28, 0.81, 0.07, 0.51, 0.64])
In [41]: M.A
Out[41]:
array([[0. , 0. , 0.28, 0. , 0. ],
[0. , 0. , 0. , 0. , 0. ],
[0. , 0.81, 0. , 0. , 0. ],
[0. , 0. , 0. , 0.07, 0.51],
[0. , 0. , 0. , 0.64, 0. ]])


Related Topics



Leave a reply



Submit