www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - select all rows (resp. columns) out of matrix

reply "seany" <seany uni-bonn.de> writes:
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] ?
Nov 14 2013
parent reply "TheFlyingFiddle" <theflyingfiddle gmail.com> writes:
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]; } }
Nov 14 2013
next sibling parent "TheFlyingFiddle" <theflyingfiddle gmail.com> writes:
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];
   }
Nov 14 2013
prev sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Nov 14 2013
next sibling parent "TheFlyingFiddle" <theflyingfiddle gmail.com> writes:
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
Nov 14 2013
prev sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
Nov 14 2013
parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
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...
Nov 14 2013