Input format
1.The first line contains T,the number of test cases
2. The following T lines contains a number n
Example:
5
1
7
10
300
55
I am new to this language i Know the simple inputs but I couldn't understand this multiple input pattern
Related
How can I convert a txt file (that has n rows of data) with the following format:
"header1","header2","header3"
1.20,2,3
2.03,3,4
1.05,5,6
8.20,9,4
etc.
into a mat file that's simply a 2xn matrix that ignores the first row and first column of the text file and essentially transposes the other columns:
2 3 5 9
3 4 6 4
There are many ways to do that, but one function in particular allows starting the read at a given row and column offset simply in the calling parameter (without having to specify headerlines or adding * in format specifier): The function dlmread
In your case, it's a simple matter of calling it with the right parameters:
M = dlmread( 'test.txt' , ',' , 1 , 1 ).' ;
M =
2 3 5 9
3 4 6 4
The nice thing about this function is it returns a double array directly (useful if you only want to read numbers), instead of a cell array like some other functions.
Note that I transposed the result at the end of the line (using the transpose operator .'), to get it the way you wanted.
Also note that the last 2 parameters are considered offset (as opposed to start row index). It means a value of 1 specify an offset of 1, so the reading will start at row index 2 (first index is 1).
I'm using awk to search a log file for a list of lines that fell between two epoch times.
This is what I've done... (I'm getting a massive list of entries)
awk '/([1480360000..1480380000])/' /Location/Data.txt
I'm also wanting to be able to replace the times with Perl Variables without having to use [0-9][0-9]and use it within a perl script. I'm puzzled.
system(awk '/([$MinRange..$MaxRange])/' /Location/Data.txt)
Thanks
This is just to give an answer based on ooags comment.
To search from a number to a number:
cat file
1 one
2 two
3 thee
4 four
5 five
6 six
7 seven
8 eight
9 nine
10 ten
awk '$1>=4 && $1<=8' file
4 four
5 five
6 six
7 seven
8 eight
Bracket are used to get from to within single letter or single number.
[a-z] from a to z
[5-8] from 5 to 8
[0-9][0-9][0-9] from 000 to 999
([0-9]|[1-9][0-9]|[1-9][0-9][0-9]) this would give from 0 to 999
etc
How do I separate the figures of specific number then make a new vecto?. This vector will include the figures that have been separated separately.
For example : if i have a number as 123456789 , what is the function or command that will separate these figures of the number so that they look like this form [1,2,3,4,5,6,7,8,9] Meaning that will turn into a vector.
Use dec2base to obtain the figures as a char vector (i.e. string), and then convert those chars into numbers with the usual trick of subtracting '0':
>> number = 123456789;
>> figures = dec2base(number,10)-'0'
figures =
1 2 3 4 5 6 7 8 9
Still new to the programing game but I need a little help! I'm not exactly sure how to describe what I want to do but I'll give it my best shot. I have a set of numbers produced by an algorithm I've put together. e.g. :
....
10 10 10
11 11 11
12 1 2
13 3 4
14 12 13
15 6 7
16 5 15
17 8 9
....
Essentially what I want to do is assign these index numbers to groups. Lets say I start with the number 14 in the first column. It is going to belong to group 1, so I label it in a new column in row 14 "1" for group one. The second and the third column show other index numbers that are grouped with the index 14. So I use a code like:
FindLHS = find(matrix(:,1)==matrix(14,2));
and
FindRHS = find(matrix(:,1)==matrix(14,3));
so clearly this will produce the results of
FindLHS = 12
FindRHS = 13
I will then proceed to label both 12 and 13 as belonging to group "1" as I did for 14
now my problem is I want to do this same procedure for both 12 and 13 of finding and labelling the indexs for 12 and 13 being (1,2) and (3,4). Is there a way to repeat that code for both idx of 1,2,3 and 4? because the real dataset has over 5000 data points in it...
Do you understand what I mean?
Thanks
James
All you really want to do is find wherever matrix(:,1) contains one of the numbers you've already found, include the numbers in the second and third columns into your group list (presuming they aren't already there), and stop when that list stops growing, right? This may not be the most efficient way of doing it but it gives you the basic idea:
while ~(numel(oldnum)==numel(num))
oldnum = num;
idx = ismember(matrix(:,1),oldnum)
num = unique(matrix(idx,:))
end
Output:
num =
1
2
3
4
12
13
14
Now if your first column is literally just your numbers 1 through 5000 in order, you don't need to even find the index, you can just use your number list directly.
To do this for multiple groups you would just need an outer loop that stores the information for each group, then picks out the next unused number. I'm presuming that your individual groups are consistent so that no matter which of those numbers you pick you end up with the same result - e.g. starting at 2 or 14 gives you the same result (if not, it becomes more complex).
a. I need help to write a function that takes the name of a tab delimited text file and returns a 2-dimensional array of the data in the file, skipping the first row of data?
b. Using the same function from the first part I need to write another function maxMerge that takes the name of two of these tab-delimited files and returns a single 2-dimensional array where each element of the array is the larger of the corresponding elements from the two data files.
I would appreciate any help....Thank You!
example:
file 1 file 2 maxMerge
0 0 0 0 0 0
10 20 30 2 4 8 10 20 30
45 55 63 16 32 64 45 55 64
80 90 99 128 56 500 128 90 500
This reads a little like homework so I'll point you in some directions. The TEXTSCAN function will read the contents of a file. Have a look at the HeaderLines option for skipping the first row.
In the second part, you could do the following:
Use the function of the first part to load the two data files of size Nx2.
Concatenate the two loaded arrays so you have a Nx2x2 array.
Use the MAX function with the dimension argument to find the maximum of the concatenated array along the 3rd dimension, i.e the dimension which designates unique data files.