Fixed and simplified Matlab code and added further block-related examples

(transplanted from 276801b25a
)
This commit is contained in:
Christoph Hertzberg 2013-11-29 19:54:01 +01:00
parent baf2b13589
commit 7c1fc0ee7c

View File

@ -16,8 +16,8 @@ double s;
// Basic usage // Basic usage
// Eigen // Matlab // comments // Eigen // Matlab // comments
x.size() // length(x) // vector size x.size() // length(x) // vector size
C.rows() // size(C)(1) // number of rows C.rows() // size(C,1) // number of rows
C.cols() // size(C)(2) // number of columns C.cols() // size(C,2) // number of columns
x(i) // x(i+1) // Matlab is 1-based x(i) // x(i+1) // Matlab is 1-based
C(i,j) // C(i+1,j+1) // C(i,j) // C(i+1,j+1) //
@ -51,20 +51,34 @@ v.setLinSpace(size,low,high) // v = linspace(low,high,size)'
// Eigen // Matlab // Eigen // Matlab
x.head(n) // x(1:n) x.head(n) // x(1:n)
x.head<n>() // x(1:n) x.head<n>() // x(1:n)
x.tail(n) // N = rows(x); x(N - n: N) x.tail(n) // x(end - n + 1: end)
x.tail<n>() // N = rows(x); x(N - n: N) x.tail<n>() // x(end - n + 1: end)
x.segment(i, n) // x(i+1 : i+n) x.segment(i, n) // x(i+1 : i+n)
x.segment<n>(i) // x(i+1 : i+n) x.segment<n>(i) // x(i+1 : i+n)
P.block(i, j, rows, cols) // P(i+1 : i+rows, j+1 : j+cols) P.block(i, j, rows, cols) // P(i+1 : i+rows, j+1 : j+cols)
P.block<rows, cols>(i, j) // P(i+1 : i+rows, j+1 : j+cols) P.block<rows, cols>(i, j) // P(i+1 : i+rows, j+1 : j+cols)
P.row(i) // P(i+1, :)
P.col(j) // P(:, j+1)
P.leftCols<cols>() // P(:, 1:cols)
P.leftCols(cols) // P(:, 1:cols)
P.middleCols<cols>(j) // P(:, j+1:j+cols)
P.middleCols(j, cols) // P(:, j+1:j+cols)
P.rightCols<cols>() // P(:, end-cols+1:end)
P.rightCols(cols) // P(:, end-cols+1:end)
P.topRows<rows>() // P(1:rows, :)
P.topRows(rows) // P(1:rows, :)
P.middleRows<rows>(i) // P(:, i+1:i+rows)
P.middleRows(i, rows) // P(:, i+1:i+rows)
P.bottomRows<rows>() // P(:, end-rows+1:end)
P.bottomRows(rows) // P(:, end-rows+1:end)
P.topLeftCorner(rows, cols) // P(1:rows, 1:cols) P.topLeftCorner(rows, cols) // P(1:rows, 1:cols)
P.topRightCorner(rows, cols) // [m n]=size(P); P(1:rows, n-cols+1:n) P.topRightCorner(rows, cols) // P(1:rows, end-cols+1:end)
P.bottomLeftCorner(rows, cols) // [m n]=size(P); P(m-rows+1:m, 1:cols) P.bottomLeftCorner(rows, cols) // P(end-rows+1:end, 1:cols)
P.bottomRightCorner(rows, cols) // [m n]=size(P); P(m-rows+1:m, n-cols+1:n) P.bottomRightCorner(rows, cols) // P(end-rows+1:end, end-cols+1:end)
P.topLeftCorner<rows,cols>() // P(1:rows, 1:cols) P.topLeftCorner<rows,cols>() // P(1:rows, 1:cols)
P.topRightCorner<rows,cols>() // [m n]=size(P); P(1:rows, n-cols+1:n) P.topRightCorner<rows,cols>() // P(1:rows, end-cols+1:end)
P.bottomLeftCorner<rows,cols>() // [m n]=size(P); P(m-rows+1:m, 1:cols) P.bottomLeftCorner<rows,cols>() // P(end-rows+1:end, 1:cols)
P.bottomRightCorner<rows,cols>() // [m n]=size(P); P(m-rows+1:m, n-cols+1:n) P.bottomRightCorner<rows,cols>() // P(end-rows+1:end, end-cols+1:end)
// Of particular note is Eigen's swap function which is highly optimized. // Of particular note is Eigen's swap function which is highly optimized.
// Eigen // Matlab // Eigen // Matlab
@ -125,10 +139,8 @@ int r, c;
// Eigen // Matlab // Eigen // Matlab
R.minCoeff() // min(R(:)) R.minCoeff() // min(R(:))
R.maxCoeff() // max(R(:)) R.maxCoeff() // max(R(:))
s = R.minCoeff(&r, &c) // [aa, bb] = min(R); [cc, dd] = min(aa); s = R.minCoeff(&r, &c) // [s, i] = min(R(:)); [r, c] = ind2sub(size(R), i);
// r = bb(dd); c = dd; s = cc s = R.maxCoeff(&r, &c) // [s, i] = max(R(:)); [r, c] = ind2sub(size(R), i);
s = R.maxCoeff(&r, &c) // [aa, bb] = max(R); [cc, dd] = max(aa);
// row = bb(dd); col = dd; s = cc
R.sum() // sum(R(:)) R.sum() // sum(R(:))
R.colwise().sum() // sum(R) R.colwise().sum() // sum(R)
R.rowwise().sum() // sum(R, 2) or sum(R')' R.rowwise().sum() // sum(R, 2) or sum(R')'