![]() |
Prev | Next | extend |
Matlab or Octave |
x( i, n + 1) = y
|
C++ |
x.resize( m, n + 1)
|
x( i, n) = y
|
x(
i,
n + 1) =
y
extends x to an @(@
m \times (n+1)
@)@ matrix
and sets the element with index @(@
(i, n+1)
@)@
to have value y.
function [ok] = extend_ok()
ok = true;
m = 2;
n = 3;
for i = 1 : m
for j = 1 : n
x(i, j) = i + j;
end
end
% -----------------------
for i = 1 : m
x(i, n + 1) = i + (n + 1);
end
% -----------------------
[m, n] = size(x);
ok = ok & (m == 2);
ok = ok & (n == 4);
for i = 1 : m
for j = 1 : n
ok = ok & (x(i, j) == i + j);
end
end
return
matrix<double>
,
i is a size_t
index between zero and @(@
m - 1
@)@,
and y has type double
,
the C++ syntax
x.resize(
m,
n + 1)
x(
i,
n) =
y
extends the matrix x to have size @(@
m \times (n + 1)
@)@
and sets the element with index @(@
(i, n)
@)@
to have value y.
# include <mat2cpp.hpp>
bool extend_ok(void)
{ using namespace mat2cpp;
bool ok = true;
size_t i, j, m(2), n(3);
matrix<double> x(m, n);
for(i = 0; i < m; i++)
{ for(j = 0; j < n; j++)
x(i, j) = double(i+j);
}
// -------------------------------
x.resize(m, n + 1);
for(i = 0; i < m; i++)
x(i, n) = double(i + n);
// -------------------------------
ok &= (x.size1() == m);
ok &= (x.size2() == n + 1);
for(i = 0; i < m; i++)
{ for(j = 0; j < n; j++)
ok &= (x(i, j) == double(i+j));
}
return ok;
}