Thread overview
select all rows (resp. columns) out of matrix
Nov 14, 2013
seany
Nov 14, 2013
TheFlyingFiddle
Nov 14, 2013
TheFlyingFiddle
Nov 14, 2013
Ali Çehreli
Nov 14, 2013
TheFlyingFiddle
Nov 14, 2013
bearophile
Nov 14, 2013
H. S. Teoh
November 14, 2013
How do I access All rows (resp. columns ) of a Matrix / multidimensional array?

In scilab, you write array_var(:,row_index), colon meaning all columns, at that particular row given by row index will be selected.

can you, forexample, in D, write, array_var[0][] to select all elements (columns, in this case i believe?) of array_var[0] ?

November 14, 2013
On Thursday, 14 November 2013 at 22:06:28 UTC, seany wrote:
> How do I access All rows (resp. columns ) of a Matrix / multidimensional array?
>
> In scilab, you write array_var(:,row_index), colon meaning all columns, at that particular row given by row index will be selected.
>
> can you, forexample, in D, write, array_var[0][] to select all elements (columns, in this case i believe?) of array_var[0] ?

unittest
{
  int[][] matrix = new int[][](n,m);

  //Getting a collumn is simple.
  int[] collumn0 = matrix[0];

  //Getting a row is trickier
  int[] row0;

  //Loop over all collumns
  foreach(i; matrix) {
    row0 ~= matrix[i][0];
  }
}


November 14, 2013
On Thursday, 14 November 2013 at 22:18:44 UTC, TheFlyingFiddle wrote:
> unittest
> {
>   int[][] matrix = new int[][](n,m);
>
>   //Getting a collumn is simple.
>   int[] collumn0 = matrix[0];
>
>   //Getting a row is trickier
>   int[] row0;
>
>   //Loop over all collumns
>   foreach(i; matrix) {
>     row0 ~= matrix[i][0];
>   }
> }

Sry the way you get rows is wrong.
It should be

int[] row0;
foreach(collumn; matrix)
    row0 ~= collumn[0];

and not

>   int[] row0;
>
>   //Loop over all collumns
>   foreach(i; matrix) {
>     row0 ~= matrix[i][0];
>   }
November 14, 2013
On 11/14/2013 02:18 PM, TheFlyingFiddle wrote:
> On Thursday, 14 November 2013 at 22:06:28 UTC, seany wrote:
>> How do I access All rows (resp. columns ) of a Matrix /
>> multidimensional array?
>>
>> In scilab, you write array_var(:,row_index), colon meaning all
>> columns, at that particular row given by row index will be selected.
>>
>> can you, forexample, in D, write, array_var[0][] to select all
>> elements (columns, in this case i believe?) of array_var[0] ?
>
> unittest
> {
>    int[][] matrix = new int[][](n,m);
>
>    //Getting a collumn is simple.
>    int[] collumn0 = matrix[0];
>
>    //Getting a row is trickier
>    int[] row0;
>
>    //Loop over all collumns
>    foreach(i; matrix) {
>      row0 ~= matrix[i][0];
>    }
> }
>
>

transversal() is useful too: :)

  http://dlang.org/phobos/std_range.html#transversal

Ali

November 14, 2013
On Thursday, 14 November 2013 at 22:26:03 UTC, Ali Çehreli wrote:
> transversal() is useful too: :)
>
>   http://dlang.org/phobos/std_range.html#transversal
>
> Ali

Did not know about this one. Thanks for pointing it out :D

November 14, 2013
Ali Çehreli:

> transversal() is useful too: :)
>
>   http://dlang.org/phobos/std_range.html#transversal

transversal() is sometimes useful, but if you need a significant amount of matrix processing, slicing and dicing matrices all the time in scientific code, then probably it's better to use a custom matrix library for D. Eventually one "winner" of such libraries should come out for D (as NumPy is in Python world). But to implement such libraries D should improve its management of "$" and "..." more for user-defined types.

Bye,
bearophile
November 14, 2013
On Fri, Nov 15, 2013 at 12:13:11AM +0100, bearophile wrote:
> Ali Çehreli:
> 
> >transversal() is useful too: :)
> >
> >  http://dlang.org/phobos/std_range.html#transversal
> 
> transversal() is sometimes useful, but if you need a significant amount of matrix processing, slicing and dicing matrices all the time in scientific code, then probably it's better to use a custom matrix library for D. Eventually one "winner" of such libraries should come out for D (as NumPy is in Python world). But to implement such libraries D should improve its management of "$" and "..." more for user-defined types.
[...]

'$' already works for multidimensional arrays, you just have to implement opDollar appropriately.

Slicing syntax isn't, though. Kenji has a pull for it, but it hasn't been merged yet for whatever reason.


T

-- 
Once bitten, twice cry...