Is there any faster way to convert strings into their integer format in python? - python-3.7

Is there is a similar way to achieve line 3, more efficiently without using a nested loop?
T = int(input())
for i in range(T):
M = sum([int(i) for i in input().split()])

Related

Maximum value of fields of nested structure

I have following structure:
S.s1.val = 1;
S.s2.val= 5;
S.s3.val= 4;
...
S.s10.value = 3;
How can I find max value of all val fields without using loops. And what is general solution to apply functions to all nested structure fields?
There is no general solution, but one way to think of is structfun to collect the data you want to process to an array.
maxval = max( structfun(#(x) x.val, S) )
Internally structfun works serially like a loop, so if you're really into speed, don't use structs (or cell arrays).

How to extract columns of data from .txt files MATLAB

I have some data in a .txt file. that are separated by commas.
for example:
1.4,2,3,4,5
2,3,4.2,5,6
24,5,2,33.4,62
what if you want the average of columns, like first column (1.4,2 and 24)? or second column(2,3 and 5)?
I think putting the column in an array and using the built in mean function would work, but so far, I am only able to extract rows, not columns
instead of making another thread, I thought i'd edit this one. I am working on getting the average of each column of the well known iris data set.
I cut a small portion of the data:
5.1,3.5,1.4,0.2,Iris-setosa
4.9,3.0,1.4,0.2,Iris-setosa
4.7,3.2,1.3,0.2,Iris-setosa
4.6,3.1,1.5,0.2,Iris-setosa
5.0,3.6,1.4,0.2,Iris-setosa
5.4,3.9,1.7,0.4,Iris-setosa
4.6,3.4,1.4,0.3,Iris-setosa
delimiterln= ',';
data = importdata('iris.txt', delimiterln);
meanCol1 = mean(data(:,1))
meanCol2 = mean(data(:,2))
meanCol3 = mean(data(:,3))
meanCol4 = mean(data(:,4))
Undefined function 'sum' for input arguments of type 'cell'.
Error in mean (line 115)
y = sum(x, dim, flag)/size(x,dim);
Error in irisData(line 6)
meanCol1 = mean(data(:,1))
it looks like there is an error with handling data type...any thoughts on this? I tried getting rid of the last column, which are strings. and it seems to work without error. So i am thinking that it's because of the strings.
Use comma separated file reading function:
M = csvread(filename);
Now you have the matrix M:
col1Mean=mean(M(:,1));

Conditional statement in matlab function argument

I am wondering if its possible to have conditional statements in a function argument.
for ex,
testarray = [1,5,8,5,7,23,61,16]
psum = sum(testarray>2 & testarray<10)
will it possible to implement something like this in matlab.
I would really appreciate an example.
Yes, please see the example below using your data.
testarray = [1,5,8,5,7,23,61,16]; % your array
Find sum of all numbers greater than 2 and less than 10 in testarray
psum = sum(testarray(testarray>2 & testarray<10));
The idea is that you find the indices of the numbers that meet the condition (i.e., testarray>2 & testarray<10 in this case), extract the numbers by indexing into testarray, and then sum them.

Importing data with engineering notation into Matlab

I've got a .xls file and I want to import it into Matlab by xlsread function..I get NaNs for numbers with engineering notation..like I get NaNs for 15.252 B or 1.25 M
Any suggestions?
Update: I can use [num,txt,raw] = xlsread('...') and the raw one is exactly what I want but how can I replace the Ms with (*106)?
First you could extract everything from excel in a cell array using
[~,~,raw] = xlsread('MyExcelFilename.xlsx')
Then you could write a simple function that returns a number from the string based on 'B', 'M' and so on. Here is such an example:
function mynumber = myfunc( mystring )
% get the numeric part
my_cell = regexp(mystring,'[0-9.]+','match');
mynumber = str2double(my_cell{1});
% get ending characters
my_cell = regexp(mystring,'[A-z]+','match');
mychars = my_cell{1};
% multiply the number based on char
switch mychars
case 'B'
mynumber = mynumber*1e9;
case 'M'
mynumber = mynumber*1e6;
otherwise
end
end
Of course there are other methods to split the numeric string from the rest, use what you want. For more info see the regexp documentation. Finally use cellfun to convert cell array to numeric array:
my_array = cellfun(#myfunc,raw);
EDIT:
Matlab does not offer any built-in formatting of strings in engineering format.
Source: http://se.mathworks.com/matlabcentral/answers/892-engineering-notation-printed-into-files
In the source you will find also function which would be helpful for you.

Mapping letters to integers in MATLAB

The function arithenco needs the input message to be a sequence of positive integers. Hence, I need convert a message into a sequence of numbers message_int, by using the following mapping.
‘A’→1, ‘C’→2, ‘G’→3, ‘T’→4.
From what I understand, the alphabet you are using contains only four values A,C,G,T (DNA sequences I suppose).
Simple comparison would suffice:
seq = 'TGGAGGCCCACAACCATTCCCTCAGCCCAATTGACCGAAAGGGCGCGA';
msg_int = zeros(size(seq));
msg_int(seq=='A') = 1;
msg_int(seq=='C') = 2;
msg_int(seq=='G') = 3;
msg_int(seq=='T') = 4;
Oh, just reread your question: your mapping is not so simple. Sorry.
(since darvidsOn wrote the same I won't delete this answer - it might give you a start - but it doesn't answer your question completely).
Have a look at http://www.matrixlab-examples.com/ascii-chart.html
You can use d = double('A') to convert a char into a double- you will then need to subtract 64 to get the mapping that you want (because A is ascii code 65).