Matrix input in Julia? - matrix

In Julia, I would like to write a function that prompts the user multiple times for matrix input, and then stores their inputs into an array. I have tried the following so far:
function acceptlist()
matrix_array=[]
while true:
matrix_input=read() #don't know what function to use?
if matrix_input="quit"
break
end
push!(matrix_array, matrix_input)
end
end
However, I am not sure how to accept matrix inputs in the way I desire. Is there any way for me to accept matrix inputs from the user? Also, I would like to user to NOT have to manually enter the matrices into the function (using readdlm or something similar). For example, I want the user to be able to read in the matrix from another file, assign it to some variable, and then enter that variable into this function acceptlist() as user input.

This code might provide an example of what you need (sorry, but a bit tired for a full answer):
m = Matrix{Int}(undef,0,0)
while true
s = readline()
if length(s)<1
break
end
r = hcat(parse.(Int, split(s))...)
m = size(m,1)>0 ? vcat(m,r) : r
end
After running, m should have an Int matrix. Same logic would apply to Float64, and many more validations can be added depending on the level and context of the code.
For example, a run could look like:
1 2 3
2 3 4
4 5 6
julia> m
3×3 Matrix{Int64}:
1 2 3
2 3 4
4 5 6

If you are on Linux or Mac you can just be using DelimitedFiles and run readdlm(stdin). The user will need to press Ctrl+D once she or he is done.
See this sample Julia session on Ubuntu:
julia> readdlm(stdin;comments=true)
1 2 3
4 5 6 # I now press Ctrl+D
2×3 Matrix{Float64}:
1.0 2.0 3.0
4.0 5.0 6.0
This will however not work on Windows as creators of some operating systems never found out how to make a working text terminal ;-)

Related

Incoherent local/global line numbering in Scinotes

When I switch on the "local line numbering" option in SciNotes, I get a strange effect. The line numbers show as local (starting with the "function" line) for some of my functions, but global (counting from the beginning of the code file) for others.
My first thought was that a function wasn't ended properly (too few "end" instructions wrt the number of opened loops, ifs and other such), but then my code would crash, which it doesn't. Also, indentation looks fine when I auto-adjust it.
It doesn't very much get in my way, but I wonder if it isn't a symptom of something more serious cooking under the surface. Has anybody had a similar experience?
FWIW I'm using Scilab 6.0.2 under Windows 10.
I think I have found the source of the "problem". Line numbering only gets local in a function whose first line (the one with the "function" keyword) is not terminated with a semicolon.
Like this:
1 // This is my fantastic power program
2 clear;
3 clc;
4 cd "c:\myDir\Scilab\Sandbox\FunAndGames"
5
6 function S=square(x); // <-- now you see it
7 S=x*x; // (the semicolon, I mean)
8 endfunction;
9
1 function C=cube(x) // <-- now you don't
2 C=x*x*x;
3 endfunction;
13
14
15 // Now the body of my program:
16
17 X=zeros(5,5);
18 ....

How to initialize a simple matrix in SAS?

I am new to SAS and have been using R most of the time. I am stuck with a simple and frustrating issue. All I want to do is to create a simple 3 X 3 matrix in SAS. But it throws an error. I need some help in understanding what's going on. The SAS documentation is not very helpful.
data matrixTest;
input Y $ X;
cards;
4 0
3 1
1 1
;
run;
/*Convert X to a categorical variable*/
data matrixTest;
set matrixTest;
if X = 0 then X = "0";
else X = "1";
run;
/*Get design matrix from the regression model*/
proc transreg data=matrixTest design;
model class(X/ zero=last);
output out=input_mcmc(drop=_: Int:);
run;
mX = {5 4 3, 4 0 4, 7 10 3};
And I get the following error when creating the matrix mX:
ERROR 180-322: Statement is not valid or it is used out of proper order.
Your error is that SAS is not a matrix language. SAS is more like a database language; the unit of operation is the dataset, analogous to a SQL table or a dataframe in R or Python.
SAS does have a matrix language built into the system, SAS/IML (interactive matrix language), but it's not part of base SAS and isn't really what you use in the context you're showing. The way you enter data as part of your program is how you did it in the first data step, with datalines.
Side note: You're also showing some R tendencies in the second data step; you cannot convert a variable's type that way. SAS has only 'numeric' and 'character', so you don't have 'categorical' data type anyway; just leave it as is.
Do not use the same data set name in the SET and DATA statements. This makes it hard to debug because you've destroyed your initial data set.
You cannot change types on the fly in SAS. If a variables i character it stays character.
If a variable is numeric, you assign values without quotes, quotes are used for character variables.
Your attempt to create a categorical variable doesn't make sense given the fact that it's already 0/1. Make sure your test data is reflective of your actual situation.
I'm not familiar with PROC TRANSREG so I cannot comment on that portion but those are the issues you're facing now.
As someone else mentioned, SAS is not a matrix language, it processes data line by line instead which means it can handle really, really large data sets because it doesn't have to load it into memory.
Your data set, matrixTest is essentially a data set and ready to go. You don't need to convert it to a matrix or 'initialize' it.
If you want a data set with those values then create that as a data set:
data mx;
input var1-var3;
cards;
5 4 3
4 0 4
7 10 3
;
run;

What's the equivalent of R sample function in Stata

I have a basic question about Stata. I have programming experience in R but I've started a new job where Stata is the main language. I'm currently diving into Stata on my own and sometimes it's hard to understand how to do simple things.
I've trying to get 5 random numbers between 3 and 50 but without success.
In R, any of these would work:
floor(runif(5, min=3, max=50))
16 39 11 11 5 # output
sample(3:50, 5, replace=TRUE)
28 13 5 36 19 # output
But I'm not sure how to do this in Stata, specifically how to return random numbers within the desired range (3:50). Any pointers would be appreciated. I found the runiform() function but I don't think I can get the same output.
Is this what you want?
set obs 5
generate rnum = runiform(3, 50)
You are basically creating a dataset first and then generating a variable with the desired properties.

how to display 3 random pictures out of 8 using bash

I have 8 images in a directory.
the path is /blabla.com/img.
I need to access this path and choose 3 out of 8 randomly and display those.
If 3 pics are the same, it should echo "yeeey".
Otherwise, "neeey" and record these responses in a text file.
I am not going to do your homework for you!
However I can give you some insight:
store your 8 file names in an array
call $RANDOM % 8 3 times and store the value in 3 index variables
use the 3 index variables to extract your 3 files
use sha256sum, sha512sum or md5sum to compute the signature of your images and store the result in 3 variables
compare the values of the 3 variables if they are the same echo "yeeey" else echo "neeey"
if on top of that you want to display the picture as written in your post you could call eog or other similar tool with the finename as parameter and of course in background, with a & at the end of the command call.
Good luck with your assignment and let me know if you need help!
let's array an array of distinct elements (for example 8):
array=({A..H})
(1) use RANDOM special variable modulo the number of elements to get a random number between 0 and number-1 inclusive
number=$((RANDOM%${#array[#]}))
the first random element is
first=${array[number]}
remove the element from array and reassign the array to reindex without gap (declare -p array to see)
unset array[number]
array=("${array[#]}")
restart from (1)

Stata rnormal()

I want to generate a fixed random variable ~N(0,10) for every observation for future computation.
gen X=rnormal (0,10)
list X
Blank
How can I see what value of X is being generated?
You were probably using an empty dataset when you issued these commands. In that case you would first need to tell Stata how many observations your dataset contains. For that you need to use the set obs command, so something like:
. set seed 12345
. set obs 10
obs was 0, now 10
. gen x = rnormal(0,10)
. list, clean
x
1. -9.580833
2. -2.907274
3. 8.45202
4. 8.617108
5. -12.19151
6. 9.457337
7. 1.722469
8. -13.29949
9. -11.5291
10. 25.1646
Think of what would happen when you did not use set obs. In that case Stata would see gen x = rnormal(0,10) and think "ok, I need to create random draws from a normal distribution, but how many?". If you had a dataset open, then it would answer "as many as there are observations in the dataset". If you had no dataset open, then the answer would still be "as many as there are observations in the dataset", but that happens to be 0.
Edit:
If you just want one number you are best of using scalars and not variables. In Stata a scalar refers to a single number and a variable refers to a single column in your dataset. For scalars it is best to use temporary names, as they share the same namespace as variables but variables take precedence when it comes to abreviations, which can lead to unexpected behaviour. So you could do something like:
. tempname a
. scalar `a' = rnormal(0,10)
. di `a'
10.737423

Resources