cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
JMP is taking Discovery online, April 16 and 18. Register today and join us for interactive sessions featuring popular presentation topics, networking, and discussions with the experts.
Choose Language Hide Translation Bar
scottdprm
Level II

How to fill Matrix using a nested loop

I am trying to extract a specific set of numeric data from a file (which contains both text and numbers) and put it into a separate table which is easier to analyze in JMP.  I want the script to work for any number of columns or rows where the data meeting specific criteria might be.  What I have come up with is below:

Rowlist = [];
For( i = 1, i <= N Rows( Dtraw ), i++,
	If( i == 1,
		If( Num( Column( Dtraw, 2 )[i] ) > 0 & Num( Column( Dtraw, 2 )[i] ) < 1000,
			Rowlist = Matrix( i )
		),
		If( Num( Column( Dtraw, 2 )[i] ) > 0 & Num( Column( Dtraw, 2 )[i] ) < 1000,
			Rowlist = V Concat( Rowlist, Matrix( i ) )
		)
	)
);
show(Rowlist);

cols= dtraw<<get column names();
MainData=J(nitems(rowlist),nitems(cols)-1);

For( i = 1, i <= N Items( rowlist ), i++,
	For( k = 2, k <= N items ( cols ), i++,
		MainData[i,k] =  Num( Column( dtraw, k )[rowlist[i]] );
		
		)
	);
show(MainData);

Rowlist is the matrix of rows that meet the criteria I need (that is, they are numbers and not text).  I want to use this list and "<<get column names" to define the regions of my raw data file where the data I need resides.  I want to extract that specific data into a different table.  The first For loop for Rowlist works just fine, but the nested for loop does not do what I want and I can't figue out why.  Can anyone help me with this? 

 

 

-Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

Re: How to fill Matrix using a nested loop

You have a couple of typing errors.

1. Your second For Loop

For( k = 2, k <= N Items( cols ), i++,

you are incrementing "i", not "k".  It should be

For( k = 2, k <= N Items( cols ), k++,

Secondly, you need to subtract 1 from k when referencing your MainData matrix, otherwise you will go beyond the range, since it is 1 less than the number of columns.

MainData[i, k - 1] = Num( Column( dtraw, k - 1 )[rowlist[i]] )

The whole section should be:

For( i = 1, i <= N Rows( rowlist ), i++,
	For( k = 2, k <= N Items( cols ), k++,
		MainData[i, k - 1] = Num( Column( dtraw, k - 1 )[rowlist[i]] )
	)
);
Jim

View solution in original post

5 REPLIES 5
ian_jmp
Staff

Re: How to fill Matrix using a nested loop

Of course, the code looks 'roughly right'.

What error do you actually see, please? Can you upload a part of 'dtraw'?

scottdprm
Level II

Re: How to fill Matrix using a nested loop

I get the error message below:

 

invalid subscript (must be number or list of numbers){1271} in access or evaluation of 'Subscript' , rowlist[/*###*/i]

 

In the following script, error marked by /*###*/

 

Show( Rowlist );

cols = dtraw << get column names();

MainData = J( N Items( rowlist ), N Items( cols ) - 1 );

For( i = 1, i <= N Items( rowlist ), i++,

For( k = 2, k <= N Items( cols ), i++,

MainData[i, k] = Num( Column( dtraw, k )[rowlist[/*###*/i]] )

)

);

txnelson
Super User

Re: How to fill Matrix using a nested loop

You have a couple of typing errors.

1. Your second For Loop

For( k = 2, k <= N Items( cols ), i++,

you are incrementing "i", not "k".  It should be

For( k = 2, k <= N Items( cols ), k++,

Secondly, you need to subtract 1 from k when referencing your MainData matrix, otherwise you will go beyond the range, since it is 1 less than the number of columns.

MainData[i, k - 1] = Num( Column( dtraw, k - 1 )[rowlist[i]] )

The whole section should be:

For( i = 1, i <= N Rows( rowlist ), i++,
	For( k = 2, k <= N Items( cols ), k++,
		MainData[i, k - 1] = Num( Column( dtraw, k - 1 )[rowlist[i]] )
	)
);
Jim
scottdprm
Level II

Re: How to fill Matrix using a nested loop

Thank you this works perfectly!

scottdprm
Level II

Re: How to fill Matrix using a nested loop

Below is a generic example of the data (showing the format):

Date/Time:11/29/2017 20:11          
TitleTEXT          
TitleTEXT          
TitleTEXT          
TitleTEXT          
TitleTEXT          
TitleTEXT          
TitleTEXT          
TitleTitleTitleTitleTitleTitleTitleTitleTitleTitleTitleTitle
 11111111111
 22222222222
 33333333333
 44444444444
 55555555555
 66666666666
 77777777777
 88888888888
 99999999999
 1010101010101010101010
 1111111111111111111111
 1212121212121212121212
 1313131313131313131313
 1414141414141414141414
 1515151515151515151515
 1616161616161616161616
 1717171717171717171717
 1818181818181818181818
 1919191919191919191919
 2020202020202020202020
 2121212121212121212121
 2222222222222222222222
 2323232323232323232323
 2424242424242424242424
 2525252525252525252525
 2626262626262626262626
 2727272727272727272727
 2828282828282828282828
 2929292929292929292929
 3030303030303030303030
 3131313131313131313131
 3232323232323232323232
 3333333333333333333333
 3434343434343434343434
 3535353535353535353535
 3636363636363636363636
 3737373737373737373737
 3838383838383838383838
 3939393939393939393939
 4040404040404040404040
 4141414141414141414141
 4242424242424242424242
 4343434343434343434343
 4444444444444444444444
 4545454545454545454545
 4646464646464646464646
 4747474747474747474747
 4848484848484848484848
 4949494949494949494949
 5050505050505050505050
 5151515151515151515151
 5252525252525252525252
 5353535353535353535353
 5454545454545454545454
 5555555555555555555555
 5656565656565656565656
 5757575757575757575757
 5858585858585858585858
 5959595959595959595959
 6060606060606060606060
 6161616161616161616161
 6262626262626262626262
 6363636363636363636363
 6464646464646464646464
 6565656565656565656565
 6666666666666666666666
 6767676767676767676767
 6868686868686868686868
 6969696969696969696969
 7070707070707070707070
 7171717171717171717171
 7272727272727272727272
 7373737373737373737373
            
Value label 7474747474747474  
Value label 7575757575757575  
Value label 7676767676767676  
Value label 7777777777777777  
Value label 7878787878787878  
Value label 7979797979797979  
Value label 8080808080808080  
Value label 8181818181818181  
            
Date/Time:11/29/2017 20:11          
TitleTEXT          
TitleTEXT          
TitleTEXT          
TitleTEXT          
TitleTEXT          
TitleTEXT          
TitleTEXT          
TitleTitleTitleTitleTitleTitleTitleTitleTitleTitleTitleTitle
 11111111111
 22222222222
 33333333333
 44444444444
 55555555555
 66666666666
 77777777777
 88888888888
 99999999999
 1010101010101010101010
 1111111111111111111111
 1212121212121212121212
 1313131313131313131313
 1414141414141414141414
 1515151515151515151515
 1616161616161616161616
 1717171717171717171717
 1818181818181818181818
 1919191919191919191919
 2020202020202020202020
 2121212121212121212121
 2222222222222222222222
 2323232323232323232323
 2424242424242424242424
 2525252525252525252525
 2626262626262626262626
 2727272727272727272727
 2828282828282828282828
 2929292929292929292929
 3030303030303030303030
 3131313131313131313131
 3232323232323232323232
 3333333333333333333333
 3434343434343434343434
 3535353535353535353535
 3636363636363636363636
 3737373737373737373737
 3838383838383838383838
 3939393939393939393939
 4040404040404040404040
 4141414141414141414141
 4242424242424242424242
 4343434343434343434343
 4444444444444444444444
 4545454545454545454545
 4646464646464646464646
 4747474747474747474747
 4848484848484848484848
 4949494949494949494949
 5050505050505050505050
 5151515151515151515151
 5252525252525252525252
 5353535353535353535353
 5454545454545454545454
 5555555555555555555555
 5656565656565656565656
 5757575757575757575757
 5858585858585858585858
 5959595959595959595959
 6060606060606060606060
 6161616161616161616161
 6262626262626262626262
 6363636363636363636363
 6464646464646464646464
 6565656565656565656565
 6666666666666666666666
 6767676767676767676767
 6868686868686868686868
 6969696969696969696969
 7070707070707070707070
 7171717171717171717171
 7272727272727272727272
 7373737373737373737373
            
Value label 7474747474747474  
Value label 7575757575757575  
Value label 7676767676767676  
Value label 7777777777777777  
Value label 7878787878787878  
Value label 7979797979797979  
Value label 8080808080808080  
Value label 8181818181818181