Matlab: convert cell of char to cell of vector of doubles - matlab

I would like to convert a <1 x 8 cell> of chars
'111001' '00' '111000' '01' '1111' '10' '11101' '110'
to a <1 x 8 cell> of <1 x (length bitcode)> doubles
[111001] [00] [111000] [01] [1111] [10] [11101] [110]
How can I do this?

here's a one liner solution:
a=num2cell(str2double(s))

s = {'111001', '00', '111000', '01', '1111', '10', '11101', '110'};
d = cellfun(#(c_) c_ - '0', s, 'UniformOutput', false);
'01234' - '0' will give 1 by 5 double matrix [0, 1, 2, 3, 4] because '01234' is actually char(['0', '1', '2', '3', '4']), and minus operation between characters will give the operation between their ASCII codes.

Try this:
s = {'111001','00','111000','01','1111','10','11101','110'}
num = str2num(str2mat(s));

Try using str2num to convert char arrays (strings) to numbers.
If you want to interpret the numbers as binary (base 2) numbers, try using bin2dec.

Related

Number of Zeros after a non Zero number

Guys I have a business case where I need to count the number of Zeros after a non Zero number in a column with transaction values in qliksense. For example,
1,000 = 3 10,000 = 4 10,500 = 2 11,510 = 1
23,415 = 0
I have tried various codes but nothing has worked so far.
Can anyone help?
Convert Value to Text,
Find position of last occurrence of 1-9 using FindOneOf,
Take part of the string after position using Mid (we need to add 1 to get after),
Check length of our 0 only string using Len
Here is the code:
Len(Mid(Text(Value), FindOneOf(Text(Value), '123456789', -1)+1))
One (dummy) way is to first find the last possition of any non-zero number in the string. Then to find the max possition of these and use the result to substring the main value and count the rest.
For example: if we have 11510
we'll find the last possition of each non-zero numer
Num LastPostion
1 4
2 0
3 0
4 0
5 3
6 0
7 0
8 0
9 0
Then we have to find the max value. In our case this is 4.
After that we'll use mid() and len() functions
mid(11510, 4 + 1) - this will return 0
and we'll get the length (len(mid(11510, 4 + 1))). This will result in 1
The result of the script below will be:
RawData:
Load * Inline [
Value
1000
10000
10500
11510
23415
];
join
Load
Value,
RangeMax(One, Two, Three, Four, Five, Six, Seven, Eight, Nine) as MaxNonZero
;
Load
Value,
Index(Value, '1', -1) as One,
Index(Value, '2', -1) as Two,
Index(Value, '3', -1) as Three,
Index(Value, '4', -1) as Four,
Index(Value, '5', -1) as Five,
Index(Value, '6', -1) as Six,
Index(Value, '7', -1) as Seven,
Index(Value, '8', -1) as Eight,
Index(Value, '9', -1) as Nine
Resident
RawData
;
NoConcatenate
Data:
Load
Value,
len(mid(Value, MaxNonZero + 1)) as NumberOfZeros
Resident
RawData
;
Drop Table RawData;

Multi hot encoding

I am new+ to Python. I am working on multi label classification and need to prepare target data for multi hot encoding. It has taken way more time than I had initially thought. This is not real data (I can't post it). The data has ID, Category that makes a row unique. So there are multiple rows for each ID for 10 categories in reality but I am mocking 4. There are bunch of other columns (predictors). Below is what I got best out of my couple of hours after trying many approaches:
test_data = pd.DataFrame()
test_data["Category"] = ['A','B','C','D','A','C']
test_data["ID"] = [1,1,3,4,5,6]
test_data =test_data.pivot(index='ID', columns="Category",
values='Category').reset_index()
test_data =test_data.fillna('0')
test_data = test_data.reset_index(drop=True).rename_axis(None, axis=1)
data = test_data.drop(['ID'], axis=1)
print(data)
ignore '=' below I don't know how to indent with space.
= A B C D
0 A B 0 0
1 0 0 C 0
2 0 0 0 D
3 A 0 0 0
4 0 0 C 0
As you can see I am filling the categories that are not present with dummy '0'.
data = data.astype(str).values
data
array(
[['A', 'B', '0', '0'],
['0', '0', 'C', '0'],
['0', '0', '0', 'D'],
['A', '0', '0', '0'],
['0', '0', 'C', '0']], dtype=object)
from sklearn.preprocessing import MultiLabelBinarizer
cat =['A','B','C','D','0']
mlb = MultiLabelBinarizer(cat)
mlb.fit_transform(data)
array(
[[1, 1, 0, 0, 1],
[0, 0, 1, 0, 1],
[0, 0, 0, 1, 1],
[1, 0, 0, 0, 1],
[0, 0, 1, 0, 1]])
There are two things I am looking for help:
How do I get rid of my dummy category encoding ('0')
It appears I am hacking my way through it. Is there a better way of doing it ?
Just for curious minds, I am using a fully connected neural network for this classification.
Thanks for your help.

MATLAB fprintf Increase Number of Digits of Exponent

If we have
A=[100 -0.1 0];
B=[30 0.2 -2]; t1='text 1'; t2=text 2'
how to use fprintf so that the output saved in a file will look like that
100 -1.000E-0001 0.000E-0000 'text 1'
30 2.000E-0001 -2.000E-0000 'text 2'
I put together a "one-liner" (spread across several lines for better readability) that takes an array, a single number format, and a delimiter and returns the desired string. And while you found the leading blank-space flag, I prefer the + flag, though the function will work with both:
A=[-0.1 0];
B=[0.2 -2];
minLenExp = 4;
extsprintf = #(num,fmt,delim) ...
strjoin(cellfun(...
#(toks)[toks{1},repmat('0',1,max([0,minLenExp-length(toks{2})])),toks{2}],...
regexp(sprintf(fmt,num),'([+-\s][\.\d]+[eE][+-])(\d+)','tokens'),...
'UniformOutput',false),delim);
Astr = extsprintf(A,'%+.4E',' ');
Bstr = extsprintf(B,'%+.4E',' ');
disp([Astr;Bstr]);
Running this yields:
>> foo
-1.0000E-0001 +0.0000E+0000
+2.0000E-0001 -2.0000E+0000
(foo is just what the script file is called.)
Here's a more general approach that searches for the exponential format instead of assuming it:
A=[100 -0.1 0].';
B=[30 0.2 -2];
extsprintf = #(fmt,arr) ...
regexprep(...
sprintf(fmt,arr),...
regexprep(regexp(sprintf(fmt,arr),'([+-\s][\.\d]+[eE][+-]\d+)','match'),'([\+\.])','\\$1'),...
cellfun(#(match)...
cellfun(...
#(toks)[toks{1},repmat('0',1,max([0,minLenExp-length(toks{2})])),toks{2}],...
regexp(match,'([+-\s][\.\d]+[eE][+-])(\d+)','tokens'),...
'UniformOutput',false),...
regexp(sprintf(fmt,arr),'([+-\s][\.\d]+[eE][+-]\d+)','match')));
fmt = '%3d %+.4E %+.4e';
disp(extsprintf(fmt,A));
disp(extsprintf(fmt,B));
Outputs
>> foo
100 -1.0000E-0001 +0.0000e+0000
30 +2.0000E-0001 -2.0000e+0000

Extract numbers from string in MATLAB

I'm working with sscanf to extract a number from a string. The strings are usually in the form of:
'44 ppm'
'10 gallons'
'23.4 inches'
but ocassionally they are in the form of:
'<1 ppm'
If I use the following code:
x = sscanf('1 ppm','%f')
I get an output of
1
But if I add the less than sign in front of the one:
x = sscanf('<1 ppm','%f')
I get:
[]
How can I write this code so this actually produces a number? I'm not sure yet what number it should print...but let's just say it should print 1 for the moment.
You can use regexp:
s= '<1 ppm';
x=regexp(s, '.*?(\d+(\.\d+)*)', 'tokens' )
x{1}
Demo :
>> s= {'44 ppm', '10 gallons', '23.4 inches', '<1 ppm' } ;
>> x = regexp(s, '.*?(\d+(\.\d+)*)', 'tokens' );
>> cellfun( #(x) disp(x{1}), x ) % Demo for all
'44'
'10'
'23.4'
'1'

How can we assign letters to numbers

I have the following:
d=[1 2 3 4 5 6 7]
I want Matlab to assign a day name to every number by doing a loop or
any suitable method as follows:
1 =tuesday
2=wednesday
.
.
.
7=monday
the results I am aiming to get after running the program is :
the Matlab window asks the user to enter a number from 1 to 7
n=('enter a number from 1 to 7')
then,
if we enter ,for example, 4 , this means that the printed result is: Friday
or
if we entered , for example , 7, this means that the printed result is: Monday
and so on
Is there any way to do this
regards
You could use a cell array, which allows you to store an array of text strings. The curly bracket is the key:
>> weekdays = {'Mon', 'Tues', 'Weds', 'Thurs', 'Fri', 'Sat', 'Sun'};
>> weekdays{4}
ans =
Thurs
Edit: You can get the relevant number from the user by using MATLAB's input function:
n = input('Enter your number:');
disp(weekdays{n})
Using a map might be one approach:
weekDays = containers.Map({1, 2, 3, 4, 5, 6, 7} , ...
{'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'});
number = input('enter a number from 1 to 7');
disp(sprintf('You did choose %s\n', weekDays(number)));
EDIT:
Using the solution by Bill Cheatham you end up with
weekdays = {'Mon', 'Tues', 'Weds', 'Thurs', 'Fri', 'Sat', 'Sun'};
number = input('enter a number from 1 to 7');
disp(sprintf('You did choose %s\n', weekdays{number}));