## digitalmars.D.learn - mir.ndslice: assign a vector to a matrix row

• David (21/21) Dec 15 2018 Hi
• 9il (4/25) Dec 26 2018 matrix[] = vector;
• David (4/37) Dec 27 2018 great many thanks!! Is there any logic why getting a row works by
• 9il (12/54) Dec 28 2018 This case gets a slice of a row, it does not copy the data. So
• 9il (2/26) Dec 28 2018 EDIT: a slice on the LEFT side
• David (2/26) Dec 28 2018 Many thanks for this background!!
```Hi

I am wondering if it is possible to assign a vector to a row of a
matrix?

============ main.d ==========
import mir.ndslice;

void main() {

auto matrix = slice!double(3, 4);
matrix[] = 0;
matrix.diagonal[] = 1;

auto row = matrix;
row = 4;
assert(matrix[0, 3] == 4);

// assign it to rows of a matrix?
auto vector = sliced!(double)([10, 11, 12, 13]);

// ??? Here I would like to assign the vector to the last (but
it is not working)
// matrix = vector;
}
============

So I am wondering what the correct way is to do such an
assignment without looping?
```
Dec 15 2018
```On Saturday, 15 December 2018 at 19:04:37 UTC, David wrote:
Hi

I am wondering if it is possible to assign a vector to a row of
a matrix?

============ main.d ==========
import mir.ndslice;

void main() {

auto matrix = slice!double(3, 4);
matrix[] = 0;
matrix.diagonal[] = 1;

auto row = matrix;
row = 4;
assert(matrix[0, 3] == 4);

// assign it to rows of a matrix?
auto vector = sliced!(double)([10, 11, 12, 13]);

// ??? Here I would like to assign the vector to the last
(but it is not working)
// matrix = vector;
}
============

So I am wondering what the correct way is to do such an
assignment without looping?

matrix[] = vector;

Or

matrix[2,0..\$] = vector;
```
Dec 26 2018
```On Wednesday, 26 December 2018 at 18:59:25 UTC, 9il wrote:
On Saturday, 15 December 2018 at 19:04:37 UTC, David wrote:
Hi

I am wondering if it is possible to assign a vector to a row
of a matrix?

============ main.d ==========
import mir.ndslice;

void main() {

auto matrix = slice!double(3, 4);
matrix[] = 0;
matrix.diagonal[] = 1;

auto row = matrix;
row = 4;
assert(matrix[0, 3] == 4);

// assign it to rows of a matrix?
auto vector = sliced!(double)([10, 11, 12, 13]);

// ??? Here I would like to assign the vector to the last
(but it is not working)
// matrix = vector;
}
============

So I am wondering what the correct way is to do such an
assignment without looping?

matrix[] = vector;

Or

matrix[2,0..\$] = vector;

great many thanks!! Is there any logic why getting a row works by

auto row = matrix;

but assigning to a row works (only) by the two variant you posted?
```
Dec 27 2018
```On Thursday, 27 December 2018 at 21:17:48 UTC, David wrote:
On Wednesday, 26 December 2018 at 18:59:25 UTC, 9il wrote:
On Saturday, 15 December 2018 at 19:04:37 UTC, David wrote:
Hi

I am wondering if it is possible to assign a vector to a row
of a matrix?

============ main.d ==========
import mir.ndslice;

void main() {

auto matrix = slice!double(3, 4);
matrix[] = 0;
matrix.diagonal[] = 1;

auto row = matrix;
row = 4;
assert(matrix[0, 3] == 4);

// assign it to rows of a matrix?
auto vector = sliced!(double)([10, 11, 12, 13]);

// ??? Here I would like to assign the vector to the last
(but it is not working)
// matrix = vector;
}
============

So I am wondering what the correct way is to do such an
assignment without looping?

matrix[] = vector;

Or

matrix[2,0..\$] = vector;

great many thanks!! Is there any logic why getting a row works
by

auto row = matrix;

but assigning to a row works (only) by the two variant you
posted?

This case gets a slice of a row, it does not copy the data. So
row[i] is matrix[0, i], the same number in the RAM.

auto row = matrix;

This case gets a slice of a row, it does not copy the data.

If you wish to copy data you need to use a slice on the right
side:

row[] = matrix;

or

auto row = matrix.slice; // 'slice' allocates new data

For columns:

col[] = matrix[0 .. \$, 0];
```
Dec 28 2018
```On Friday, 28 December 2018 at 08:09:09 UTC, 9il wrote:
On Thursday, 27 December 2018 at 21:17:48 UTC, David wrote:
On Wednesday, 26 December 2018 at 18:59:25 UTC, 9il wrote:
On Saturday, 15 December 2018 at 19:04:37 UTC, David wrote:
[...]

matrix[] = vector;

Or

matrix[2,0..\$] = vector;

great many thanks!! Is there any logic why getting a row works
by

auto row = matrix;

but assigning to a row works (only) by the two variant you
posted?

This case gets a slice of a row, it does not copy the data. So
row[i] is matrix[0, i], the same number in the RAM.

auto row = matrix;

This case gets a slice of a row, it does not copy the data.

If you wish to copy data you need to use a slice on the right
side:

EDIT: a slice on the LEFT side
```
Dec 28 2018
```On Friday, 28 December 2018 at 08:11:37 UTC, 9il wrote:
On Friday, 28 December 2018 at 08:09:09 UTC, 9il wrote:
On Thursday, 27 December 2018 at 21:17:48 UTC, David wrote:
On Wednesday, 26 December 2018 at 18:59:25 UTC, 9il wrote:
[...]

great many thanks!! Is there any logic why getting a row
works by

auto row = matrix;

but assigning to a row works (only) by the two variant you
posted?

This case gets a slice of a row, it does not copy the data. So
row[i] is matrix[0, i], the same number in the RAM.

auto row = matrix;

This case gets a slice of a row, it does not copy the data.

If you wish to copy data you need to use a slice on the right
side:

EDIT: a slice on the LEFT side

Many thanks for this background!!
```
Dec 28 2018