This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Colwise multiplication with a matrix

Tags: None
(comma "," separated)
hijokpayne
Registered Member
Posts
25
Karma
0
Say I have two matrices mat1 of size 3xN and mat2 of size m x n. And N = m*n , i.e number of cols in mat1 is same as the size of mat2.

I want to multiply column 0 of a matrix mat1 with mat2(0,0), column 1 of mat1 with mat2(0,1), and so on.

If mat2 was a vector of length N, thaen I could have done
Code: Select all
mat1 = mat1 * v.asDiagonal()


How do I do this operation efficiently without creating temporaries , since my N is quite large.

Update:
One solution I found is to map mat2 to a vector by doing:
Code: Select all
Map<VectorXd> v(mat2.data(), mat2.size());
mat1 = mat1 * v.asDiagonal();


But still I would like to know if there is a better way.

P.S I have by default row_major enabled, so the above code does what i expect.

Last edited by hijokpayne on Tue Oct 29, 2013 7:05 pm, edited 1 time in total.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
Nothing better than the map trick. However, be careful of the storage order of mat2.
hijokpayne
Registered Member
Posts
25
Karma
0
Thanks. I like the .asDiagonal() functionality.

However I feel, it will be nice to have a similar .asVector() functionality for Matrix types. I mean something similar to Matlab's A ( : ).


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Yahoo [Bot]