2D matrix operation (subtraction)
septc
septcolor7 at gmail.com
Sat Feb 22 06:52:31 UTC 2020
On Friday, 21 February 2020 at 08:51:49 UTC, Andre Pany wrote:
> Hi,
>
> I have a 2D double array and I want to subtract from the first
> column a value,
> is this possible with matrix operation in D?
>
> ```
> void main()
> {
> double[][] data = [[0.0, 1.4], [1.0, 5.2], [2.0, 0.8]];
>
> // subtract -2.0 from the first column for every value
>
> // Expected output
> // data = [[-2.0, 1.4], [-1.0, 5.2], [0.0, 0.8]];
>
> }
> ```
>
> Kind regards
> André
I've recently learning Mir and so interested in
this topic. For the above purpose, I am wondering if
this Numpy-like approach is also valid:
y[ 0..$, 0 ] *= 100;
The online editor (https://run.dlang.io/) seems to give
the expected result.
// (the below code is based on the post by jmh530)
import std.stdio;
import mir.ndslice;
void main()
{
auto x = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0].sliced(3, 2);
auto y = x.dup;
writeln( x ); // [[0, 1], [2, 3], [4, 5]]
writeln( y ); // [[0, 1], [2, 3], [4, 5]]
x.byDim!1.front.each!"a *= 100";
y[ 0..$, 0 ] *= 100;
writeln( x ); // [[0, 1], [200, 3], [400, 5]]
writeln( y ); // [[0, 1], [200, 3], [400, 5]]
}
More information about the Digitalmars-d-learn
mailing list