![]() |
Prev | Next | sub |
Matlab or Octave |
z = x - y
|
C++ |
z = x - y
|
z =
x -
y
sets z to the @(@
m \times n
@)@ matrix with
the @(@
(i,j)
@)@ element given by
@[@
z_{i,j} = x_{i,j} - y_{i,j}
@]@
function [ok] = sub_ok()
ok = true;
m = 3;
n = 2;
x = rand(m, n);
y = rand(m, n);
% -------------
z = x - y;
% -------------
[m, n] = size(z);
ok = ok & (m == 3);
ok = ok & (n == 2);
for i = [1 : m]
for j = [1 : n]
zij = x(i, j) - y(i, j);
ok = ok & abs(z(i, j) - zij) < 1e-10;
end
end
return
z =
x -
y
where x, y, and z are
ublas matrix<double>
objects
with size @(@
m \times n
@)@.
# include <cmath>
# include <mat2cpp.hpp>
bool sub_ok(void)
{ bool ok = true;
using namespace mat2cpp;
size_t i, j, m(3), n(2);
matrix<double> x = rand(m, n);
matrix<double> y = rand(m, n);
// ----------------------
matrix<double> z = x - y;
// ----------------------
ok &= (z.size1() == m);
ok &= (z.size2() == n);
for(i = 0; i < m; i++)
{ for(j = 0; j < n; j++)
{ double zij = x(i,j) - y(i,j);
ok &= std::fabs(z(i, j) - zij) < 1e-10;
}
}
return ok;
}