cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Browse apps to extend the software in the new JMP Marketplace
%3CLINGO-SUB%20id%3D%22lingo-sub-792046%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%E6%9C%89%E6%B2%A1%E6%9C%89%E5%8A%9E%E6%B3%95%E4%B8%8D%E4%BD%BF%E7%94%A8%E5%BE%AA%E7%8E%AF%E6%9D%A5%E7%94%A8%E5%80%BC%20101..505%20%E5%A1%AB%E5%85%85%205x5%20%E7%9F%A9%E9%98%B5%EF%BC%9F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-792046%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3E%E8%82%AF%E5%AE%9A%E6%9C%89%E5%8A%9E%E6%B3%95%EF%BC%81%3C%2FP%3E%3CP%3E%20%3C%2FP%3E%3CP%3E%E5%BE%AA%E7%8E%AF%E7%89%88%E6%9C%AC%EF%BC%9A%3C%2FP%3E%3CP%3E%20%3C%2FP%3E%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3Eii%20%3D%201%3A%3A5%3B%20jj%20%3D%201%3A%3A5%3B%0A%0Am%20%3D%20j(5%2C5)%3B%0A%0Aforeach(%7Bi%7D%2C%20ii%2C%0A%09foreach(%7Bj%7D%2C%20jj%2C%0A%09%09m%5Bi%2Cj%5D%20%3D%20i*100%20%2B%20j%0A%09)%0A)%3B%0A%0Am%3B%0A%0A%23%3D%26gt%3B%20%5B%20101%20102%20103%20104%20105%2C%20%0A%09201%20202%20203%20204%20205%2C%20%0A%09301%20302%20303%20304%20305%2C%20%0A%09401%20402%20403%20404%20405%2C%20%0A%09501%20502%20503%20504%20505%5D%3B%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%E5%A6%82%E6%9E%9C%E6%82%A8%E6%9C%89%E5%A4%9A%E7%A7%8D%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88%EF%BC%8C%E6%AC%A2%E8%BF%8E%E6%8F%90%E4%BE%9B%EF%BC%81%E6%88%91%E6%AD%A3%E5%9C%A8%E5%B0%9D%E8%AF%95%E2%80%9C%E7%86%9F%E7%BB%83%E6%8E%8C%E6%8F%A1%E2%80%9D%E7%9F%A9%E9%98%B5%E3%80%82%3C%2FP%3E%3CP%3E%20%3C%2FP%3E%3CP%3E%20%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-792046%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CLINGO-LABEL%3E%E8%87%AA%E5%8A%A8%E5%8C%96%E5%92%8C%E8%84%9A%E6%9C%AC%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-792136%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%E5%9B%9E%E5%A4%8D%EF%BC%9A%E6%9C%89%E6%B2%A1%E6%9C%89%E5%8A%9E%E6%B3%95%E4%B8%8D%E7%94%A8%E5%BE%AA%E7%8E%AF%E5%B0%B1%E7%94%A8%E5%80%BC%20101..505%20%E5%A1%AB%E5%85%85%205x5%20%E7%9F%A9%E9%98%B5%EF%BC%9F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-792136%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3E%E7%BB%88%E4%BA%8E%E6%8B%BF%E5%88%B0%E4%BA%86%E3%80%82%E4%B8%8D%E8%BF%87%E4%B8%8D%E5%A4%AA%E5%A5%BD%E7%9C%8B%3C%2FP%3E%3CP%3E%20%3C%2FP%3E%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3Ea%20%3D%20shape(mod(0%3A%3A24%2C%205)%20%2B%201%2C%205)%3B%0A%0A%23%3D%26gt%3B%20%5B1%202%203%204%205%2C%0A1%202%203%204%205%2C%0A1%202%203%204%205%2C%0A1%202%203%204%205%2C%0A1%202%203%204%205%5D%0A%0Ab%20%3D%20shape(floor(index(10%2C%2058%2C%202)%20%2F%2010)%2C%205)%20*%20100%3B%0A%0A%23%3D%26gt%3B%20%5B100%20100%20100%20100%20100%2C%0A200%20200%20200%20200%20200%2C%0A300%20300%20300%20300%20300%2C%0A400%20400%20400%20400%20400%2C%0A500%20500%20500%20500%20500%5D%0A%0Aa%2Bb%3B%0A%0A%23%3D%26gt%3B%20%5B101%20102%20103%20104%20105%2C%0A201%20202%20203%20204%20205%2C%0A301%20302%20303%20304%20305%2C%0A401%20402%20403%20404%20405%2C%0A501%20502%20503%20504%20505%5D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%20%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-792155%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%E5%9B%9E%E5%A4%8D%EF%BC%9A%E6%9C%89%E6%B2%A1%E6%9C%89%E5%8A%9E%E6%B3%95%E4%B8%8D%E7%94%A8%E5%BE%AA%E7%8E%AF%E5%B0%B1%E7%94%A8%E5%80%BC%20101..505%20%E5%A1%AB%E5%85%85%205x5%20%E7%9F%A9%E9%98%B5%EF%BC%9F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-792155%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3Ex%20%3D%20J(%205%2C%205%2C%20.%20)%3B%0AParallel%20Assign(%20%7B%7D%2C%20x%5Br%2C%20c%5D%20%3D%20100%20*%20r%20%2B%20c%20)%3B%0Ax%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CPRE%3E%5B101%20102%20103%20104%20105%2C%0A201%20202%20203%20204%20205%2C%0A301%20302%20303%20304%20305%2C%0A401%20402%20403%20404%20405%2C%0A501%20502%20503%20504%20505%5D%3C%2FPRE%3E%0A%3CP%3E%E6%88%96%E8%80%85%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3En%20%3D%205%3B%0Ax%20%3D%201%20%3A%3A%20n%3B%0Ay%20%3D%20100%20*%20x%3B%0ARepeat(%20x%2C%20n%20)%20%2B%20Repeat(%20y%2C%20n%20)%60%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CPRE%3E%5B101%20102%20103%20104%20105%2C%0A201%20202%20203%20204%20205%2C%0A301%20302%20303%20304%20305%2C%0A401%20402%20403%20404%20405%2C%0A501%20502%20503%20504%20505%5D%3C%2FPRE%3E%0A%3CP%3E%20%3C%2FP%3E%0A%3CP%3E%20%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-792391%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%E5%9B%9E%E5%A4%8D%EF%BC%9A%E6%9C%89%E6%B2%A1%E6%9C%89%E5%8A%9E%E6%B3%95%E4%B8%8D%E7%94%A8%E5%BE%AA%E7%8E%AF%E5%B0%B1%E7%94%A8%E5%80%BC%20101..505%20%E5%A1%AB%E5%85%85%205x5%20%E7%9F%A9%E9%98%B5%EF%BC%9F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-792391%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3E(101%3A%3A105)%20%7C%2F%20(201%3A%3A205)%20%7C%2F%20(301%3A%3A305)%20%7C%2F%20(401%3A%3A405)%20%7C%2F%20(501%3A%3A505)%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-792412%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%E5%9B%9E%E5%A4%8D%EF%BC%9A%E6%9C%89%E6%B2%A1%E6%9C%89%E5%8A%9E%E6%B3%95%E4%B8%8D%E7%94%A8%E5%BE%AA%E7%8E%AF%E5%B0%B1%E7%94%A8%E5%80%BC%20101..505%20%E5%A1%AB%E5%85%85%205x5%20%E7%9F%A9%E9%98%B5%EF%BC%9F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-792412%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3E%E5%8F%AF%E8%83%BD%E4%BC%9A%E5%BE%88%E6%85%A2%EF%BC%8C%E4%BD%86%E6%82%A8%E4%B9%9F%E5%8F%AF%E4%BB%A5%E4%BD%BF%E7%94%A8%E5%B8%A6%E6%9C%89%20J()%20%E5%87%BD%E6%95%B0%E7%9A%84%20JSL%20%E4%BB%A3%E7%A0%81%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3ENames%20Default%20To%20Here(1)%3B%0A%0An%20%3D%205%3B%0A%0Ai%20%3D%201%3B%0Am%20%3D%20J(n%2C%20n%2C%20%0A%09Ceiling(i%20%2F%20n)%20*%20100%20%2B%20Mod(i%2B%2B%20-%201%2C%20n)%20%2B%201%0A)%3B%3CBR%20%2F%3E%3CBR%20%2F%3EWrite()%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%20%3C%2FP%3E%3C%2FLINGO-BODY%3E
Choose Language Hide Translation Bar
mtowle419
Level II

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
Craige_Hales
Super User

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

View solution in original post

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)

View solution in original post

jthi
Super User

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

View solution in original post

4 REPLIES 4
mtowle419
Level II

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]

 

jthi
Super User

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
Craige_Hales
Super User

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

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)