- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Are there ways to populate a 5x5 matrix with values 101..505 without using loops?
Surely there are ways!
Loop version:
ii = 1::5; jj = 1::5;
m = j(5,5);
foreach({i}, ii,
foreach({j}, jj,
m[i,j] = i*100 + j
)
);
m;
#=> [ 101 102 103 104 105,
201 202 203 204 205,
301 302 303 304 305,
401 402 403 404 405,
501 502 503 504 505];
Multiple solutions encouraged, if you have them! I'm trying to "get fluent" in matrices.
3 ACCEPTED SOLUTIONS
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: Are there ways to populate a 5x5 matrix with values 101..505 without using loops?
x = J( 5, 5, . );
Parallel Assign( {}, x[r, c] = 100 * r + c );
x;
[101 102 103 104 105, 201 202 203 204 205, 301 302 303 304 305, 401 402 403 404 405, 501 502 503 504 505]
or
n = 5;
x = 1 :: n;
y = 100 * x;
Repeat( x, n ) + Repeat( y, n )`;
[101 102 103 104 105, 201 202 203 204 205, 301 302 303 304 305, 401 402 403 404 405, 501 502 503 504 505]
Craige
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: Are there ways to populate a 5x5 matrix with values 101..505 without using loops?
(101::105) |/ (201::205) |/ (301::305) |/ (401::405) |/ (501::505)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: Are there ways to populate a 5x5 matrix with values 101..505 without using loops?
Can get slow but you can also use JSL code with J() function
Names Default To Here(1);
n = 5;
i = 1;
m = J(n, n,
Ceiling(i / n) * 100 + Mod(i++ - 1, n) + 1
);Write();
-Jarmo
4 REPLIES 4
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: Are there ways to populate a 5x5 matrix with values 101..505 without using loops?
Got one, finally. It ain't pretty though
a = shape(mod(0::24, 5) + 1, 5);
#=> [1 2 3 4 5,
1 2 3 4 5,
1 2 3 4 5,
1 2 3 4 5,
1 2 3 4 5]
b = shape(floor(index(10, 58, 2) / 10), 5) * 100;
#=> [100 100 100 100 100,
200 200 200 200 200,
300 300 300 300 300,
400 400 400 400 400,
500 500 500 500 500]
a+b;
#=> [101 102 103 104 105,
201 202 203 204 205,
301 302 303 304 305,
401 402 403 404 405,
501 502 503 504 505]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: Are there ways to populate a 5x5 matrix with values 101..505 without using loops?
Can get slow but you can also use JSL code with J() function
Names Default To Here(1);
n = 5;
i = 1;
m = J(n, n,
Ceiling(i / n) * 100 + Mod(i++ - 1, n) + 1
);Write();
-Jarmo
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: Are there ways to populate a 5x5 matrix with values 101..505 without using loops?
x = J( 5, 5, . );
Parallel Assign( {}, x[r, c] = 100 * r + c );
x;
[101 102 103 104 105, 201 202 203 204 205, 301 302 303 304 305, 401 402 403 404 405, 501 502 503 504 505]
or
n = 5;
x = 1 :: n;
y = 100 * x;
Repeat( x, n ) + Repeat( y, n )`;
[101 102 103 104 105, 201 202 203 204 205, 301 302 303 304 305, 401 402 403 404 405, 501 502 503 504 505]
Craige
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: Are there ways to populate a 5x5 matrix with values 101..505 without using loops?
(101::105) |/ (201::205) |/ (301::305) |/ (401::405) |/ (501::505)