digitalmars.D - rectangular matrix
- nix (38/38) Nov 12 2004 Hallo,
- larrycowan (4/42) Nov 12 2004 Instead of matrix[0].length = 3;
- nix (10/59) Nov 12 2004 Thank you for the hint.
- Burton Radons (27/30) Nov 12 2004 Optionally, this will require fewer allocations:
- Buchan (8/35) Nov 13 2004 Whats wrong with int[3][3] matrix? Then at least the compiler knows
Hallo, i try to built a rectangular matrix. 1 0 0 1 0 0 0 0 1 0 or 0 1 0 0 or 5 times and so on 0 0 1 0 0 1 0 0 0 0 1 int main() { int[][] matrix; matrix.length=3; matrix[0].length=3; for (int i=0; i < 3; i++){ for (int j=0; j < 3; j++){ if (i == j) { matrix[j][i] = 1; } else { matrix[j][i] = 0; } } } return 0; } I get the following error message: Error: ArrayBoundsError darr.d(11) This is line 11: matrix[j][i] = 0;
Nov 12 2004
Instead of matrix[0].length = 3; Use matrix[i].length = 3; and put it between the 2 for loops. In article <cn2bk2$7c8$1 digitaldaemon.com>, nix says...Hallo, i try to built a rectangular matrix. 1 0 0 1 0 0 0 0 1 0 or 0 1 0 0 or 5 times and so on 0 0 1 0 0 1 0 0 0 0 1 int main() { int[][] matrix; matrix.length=3; matrix[0].length=3; for (int i=0; i < 3; i++){ for (int j=0; j < 3; j++){ if (i == j) { matrix[j][i] = 1; } else { matrix[j][i] = 0; } } } return 0; } I get the following error message: Error: ArrayBoundsError darr.d(11) This is line 11: matrix[j][i] = 0;
Nov 12 2004
Thank you for the hint. In article <cn2f3d$cm5$1 digitaldaemon.com>, larrycowan says...Instead of matrix[0].length = 3; Use matrix[i].length= 3;and put it between the 2 for loops. In article<cn2bk2$7c8$1 digitaldaemon.com>, nix says...built a rectangular matrix.Hallo, i try to0 or 5 times and so on1 0 0 1 0 0 0 0 1 0 or 0 1 0main() {0 0 1 0 0 1 0 0 0 0 1 intint[][] matrix; matrix.length=3; matrix[0].length=3; for (int i=0; i < 3; i++){ for (int j=0; j < 3; j++){ if (i == j) { matrix[j][i] = 1; } else { matrix[j][i] = 0; } } } return 0; } I get the following error message: Error: ArrayBoundsError darr.d(11) This is line 11: matrix[j][i] = 0;
Nov 12 2004
larrycowan wrote:Instead of matrix[0].length = 3; Use matrix[i].length = 3; and put it between the 2 for loops.Optionally, this will require fewer allocations: template Matrix (Type) { Type [] [] allocate (size_t a, size_t b, Type [] array) { Type [] [] result; if (a && b) assert (size_t.max / a >= b); assert (array.length >= a * b); result = new Type [] [a]; size_t index = 0; foreach (inout Type [] item; result) { item = array [index .. index + b]; index += b; } return result; } Type [] [] allocate (size_t a, size_t b) { return allocate (a, b, new Type [a * b]); } } float [] [] matrix = Matrix! (float).allocate (16, 16); There should also be efficient resize and right/down-shifting (inserting space into the left or top of the matrix) functions; give me a second.
Nov 12 2004
On Fri, 12 Nov 2004 12:51:14 +0000 (UTC), nix <nix_member pathlink.com> wrote:Hallo, i try to built a rectangular matrix. 1 0 0 1 0 0 0 0 1 0 or 0 1 0 0 or 5 times and so on 0 0 1 0 0 1 0 0 0 0 1 int main() { int[][] matrix; matrix.length=3; matrix[0].length=3; for (int i=0; i < 3; i++){ for (int j=0; j < 3; j++){ if (i == j) { matrix[j][i] = 1; } else { matrix[j][i] = 0; } } } return 0; } I get the following error message: Error: ArrayBoundsError darr.d(11) This is line 11: matrix[j][i] = 0;Whats wrong with int[3][3] matrix? Then at least the compiler knows how big it is... unless you want to figure out how big they are at RT. (Its also faster and in one block of memory, meaning you can do more tricks) -- Using Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 13 2004