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

Dividing a Matrix by a vector or matrix

Tags: None
(comma "," separated)
someguy
Registered Member
Posts
14
Karma
0
Hi,

is there any way to divide each row of a matrix by a vector or each row of matrix by the corresponding row of another?

I have this code in Matlab:

Code: Select all
mxn_matrix = some_mxn_matrix + another_mxn_matrix ./ some_other_mxn_matrix;

I have been trying to learn Eigen, but I don't know how to do the above.

I have a few questions:

1) How to do the division part in Eigen?

Code: Select all
another_mxn_matrix ./ some_other_mxn_matrix

2) Is it possible to do division of each row of a matrix by a vector?


Code: Select all
% won't work in Matlab, but I am curious about Eigen
another_mxn_matrix ./ some_1xn_vector;



I tried doing the following in Eigen:

Code: Select all
another_mxn_matrix / some_1xn_vector


where

Code: Select all
some_1xn_vector is Eigen::VectorXf
another_mxn_matrix is Eigen::Matrix<some_type, Eigen::Dynamic, Eigen::Dynamic>


But I get:

error: no operator "/" matches these operands


3) Is there any equivalent of Matlab's repmat in Eigen?

Code: Select all
%This WILL work in Matlab
another_mxn_matrix ./ repmat(some_1xn_vector, m, 1);


4) Is it possible to divide a Scalar by a vector/matrix?

Code: Select all
%Matlab
testv = 3 ./ some_1xn_vector;
testm = 4 ./ some_mxn_matrix;


5) In general, what are the Eigen equivalents of the Matlab operators?
Code: Select all
./
.*
.^



Thanks in advance
jitseniesen
Registered Member
Posts
204
Karma
2
All these things are possible in Eigen and you should be able to find the syntax easily in the documentation. If you're using Eigen3 (which is currently only available as a beta version but very close to be released), here are some relevant links:


Sorry for the terse reply, but I don't have much time at the moment. If this does not help, let us know and I'll give more details later.
someguy
Registered Member
Posts
14
Karma
0
Hello,
jitseniesen wrote:All these things are possible in Eigen and you should be able to find the syntax easily in the documentation. If you're using Eigen3

No, I'm using the stable Eigen 2
jitseniesen wrote:

According to that link:

Code: Select all
#include <Eigen/Core>
#include <Eigen/Array>
...

Eigen::Matrix3f P, Q, R;
R = P.cwiseQuotient(Q);   // R = P ./ Q
R = P.array() / Q.array();


Should give me the result I wanted, but it fails to compile:

Code: Select all
1> error: class "Eigen::Matrix<float, 3, 3, 2, 3, 3>" has no member "cwiseQuotient"
1>        R = P.cwiseQuotient(Q);   // R = P ./ Q
1>              ^
1>
1> error: class "Eigen::Matrix<float, 3, 3, 2, 3, 3>" has no member "array"
1>        R = P.array() / Q.array();
1>              ^
1>
1> error: class "Eigen::Matrix<float, 3, 3, 2, 3, 3>" has no member "array"
1>        R = P.array() / Q.array();
1>                          ^


I'm using:
Intel(R) C++ Intel(R) 64 Compiler Professional for applications running on Intel(R) 64, Version 11.1, Windows 7 Pro x64

But don't blame it. My Eigen code has compiled and run fine up to now with that compiler.

So, my question remains:
How do I do R = P ./ Q ?

Thanks in advance
someguy
Registered Member
Posts
14
Karma
0
Doesn't work under Linux either:

Code: Select all
$ wget http://bitbucket.org/eigen/eigen/get/2.0.15.tar.gz
$ tar xzvf 2.0.15.tar.gz

Code: Select all
$ cat eigen_test.cpp

#include <Eigen/Core>
#include <Eigen/Array>

USING_PART_OF_NAMESPACE_EIGEN

int main(int argc, char** argv)
{
                Eigen::Matrix3f P, Q, R;
                R = P.cwiseQuotient(Q);   // R = P ./ Q
                R = P.array() / Q.array();

        return(0);
}

Code: Select all
$ g++ -c -I./eigen eigen_test.cpp
eigen_test.cpp: In function âint main(int, char**)â:
eigen_test.cpp:11: error: âclass Eigen::Matrix3fâ has no member named âcwiseQuotientâ
eigen_test.cpp:12: error: âclass Eigen::Matrix3fâ has no member named âarrayâ
eigen_test.cpp:12: error: âclass Eigen::Matrix3fâ has no member named âarrayâ


I'm using:

Code: Select all
$ uname -a
Linux machine_name 2.6.31-22-server #70-Ubuntu SMP Thu Dec 2 00:00:57 UTC 2010 x86_64 GNU/Linux

Code: Select all
$ g++ --version
g++ (Ubuntu 4.4.1-4ubuntu9) 4.4.1
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
The array syntax is for Eigen3 only. In Eigen2 you have to use the cwise() prefix syntax:

R = P .cwise()/ Q;

See: http://eigen.tuxfamily.org/dox/Tutorial ... cOperators


Bookmarks



Who is online

Registered users: bartoloni, Bing [Bot], Evergrowing, Google [Bot], ourcraft