how to do this with Matrix classes

H

harryos

i am wondering if the following array operations can be done using any
Matrix classes(colt or jama).If anyone can help ,pls advise

public double[][] arrayOp(){
double[]evalsub=new double[]{3.4,2.3,1.2};
double[][] weights=new double[][]{
{1.2,1.3,1.4},
{2.2,2.3,2.4},
{3.2,3.3,3.4},
{4.2,4.3,4.4}
};
double[][] temp=new double[weights.length][weights[0].length];
for(int i=0;i<weights.length;i++){
for(int j=0;j<evalsub.length;j++){
temp[j]=weights[j]*evalsub[j];
}
}

return temp;
}
 
U

Uwe Schmitt

i am wondering if the following array operations can be done using any
Matrix classes(colt or jama).If anyone can help ,pls advise

public double[][] arrayOp(){
double[]evalsub=new double[]{3.4,2.3,1.2};
                double[][] weights=new double[][]{
                                {1.2,1.3,1.4},
                                {2.2,2.3,2.4},
                                {3.2,3.3,3.4},
                                {4.2,4.3,4.4}
                };
                double[][] temp=new double[weights.length][weights[0].length];
                for(int i=0;i<weights.length;i++){
                        for(int j=0;j<evalsub.length;j++){
                                temp[j]=weights[j]*evalsub[j];
                        }
                }

return temp;

}


Why not ? Matrix classes expose an interface for accessing matrix
elements by
a 2d index as above. You just have to replace the types and the []
operations.

Greetings, Uwe
 
H

harryos

Why not ? Matrix classes expose an interface for accessing matrix
elements by a 2d index as above. You just have to replace the types and the [] operations.
Greetings, Uwe

thanks for the reply
but here evalsub is a 1 dimensional array..1x3 and the 2d array is
4x3..I am not sure how these can be multiplied if i make matrix
objects out of them

also I wanted to avoid use of the for loops .for large arrays that can
be slow..so i am not sure about accessing elements using 2d indices..

and the result of this calculation is a 4 x 3 matrix
4.08 2.99 1.68
7.48 5.29 2.88
10.88 7.59 4.08
14.28 9.89 5.28

i couldn't get that result from matrix operations

harry
 
U

Uwe Schmitt

On Aug 26, 9:55 pm, Uwe Schmitt <[email protected]>


Why not ? Matrix classes expose an interface for accessing matrix
elements by a 2d index as above. You just have to replace the types and the [] operations.
Greetings, Uwe

thanks for the reply
but here evalsub is a 1 dimensional array..1x3 and the 2d array is
4x3..I am not sure how these can be multiplied if i make matrix
objects out of them

also I wanted to avoid use of the for loops .for large arrays that can
be slow..so i am not sure about accessing elements using 2d indices..

and the result of this calculation is a 4 x 3 matrix
 4.08 2.99 1.68
 7.48 5.29 2.88
10.88 7.59 4.08
14.28 9.89 5.28

i couldn't get that result from matrix operations

I never used colt or jama. But libs I know can handle vectors as n x 1
matrices.
Eg matrix-vector multiplication is the same as matrix-matrix
multiplication if
you interpret the vector as a matrix.

Your code does columnwise scaling which can be expressed as

T = W D

where T = temp[][], W=weights[][] and D is a diagonal matrix with
evalsub[]s elements
on the diagonal.

This is a short formulation of your task, but needs some extra memory
for
constructing D.

Another way to describe this task and which is fast in Matlab or
Python/numpy,
is to construct a matrix E which consists of vertically stacked
evalsub[] vectors. Then

T = W .* E


where I use ".*" to describe elementwise multiplication instead of
matrix-
matrix multiplication.

Hope this helps,

Uwe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,135
Latest member
VeronaShap
Top