RE: convert array to vector

3 views
Skip to first unread message

eli mizzou

unread,
Feb 1, 2013, 8:09:38 PM2/1/13
to cognitiv...@googlegroups.com
Hi all,

I want to convert an array of integers to a vector (as gov.sandia.cognition.math.matrix.Vector)!
for example here is my array: Integer[] arr = {10, 10, 10, 5, 0, 0, 5, 10,10,10};

I appreciate any hint.

-Eli

Justin Basilico

unread,
Feb 1, 2013, 11:54:41 PM2/1/13
to cognitiv...@googlegroups.com
Hi Eli,

You can create it using a VectorFactory. We typically use one of the default ones (VectorFactory.getDenseDefault() for dense vectors or VectorFactory.getSparseDefault() for sparse vectors).

To get a feeling for the general way to construct them would be:

Vector v = VectorFactory.getDenseDefault().createVector(arr.length);
for (int i = 0; i < arr.length; i++)
{
    v.setElement(i, arr[i]);
}
// Now v is ready to go.

In the special case of an Integer array, you can turn it into a list and use one of the convenience creation methods:

Vector v1 = VectorFactory.getDenseDefault().copyValues(Arrays.asList(arr));

Thanks, : )
Justin


--
You received this message because you are subscribed to the Google Groups "Cognitive Foundry" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cognitive-foun...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages