Compare one string to all elements of an array - string

How do I get the result when '#' exist in the string will return 1 else 0. Now, I get the results of 0 0, although second string contain the character of '#'.
A = {'#'};
B = {'http://www.mathworks.com/help/matlab/ref/strcmpi.html',
'http://www.mathworks.com/help/matlab/ref/strcmpi#dfvfv.html'};
match = strcmpi(A,B)
Output:
match =
0
0
Desire Output
match =
0
1
Edit2:
why do i use the same concept as above but i get the wrong results? I want to check whether the file that store in 'data14' got 'javascript' & 'disableclick' at the same time. But the results return me all '1'.
for i = 1:4
A14 = {'javascript'};
B14 = {'disableclick'};
data14 = importdata(strcat('f14data/f14_data', int2str(i)));
feature14_data=any(cellfun(#(n) isempty(n), strfind(data14, A14{1}))) & any(cellfun(#(n) isempty(n), strfind(data14, B14{1})))
feature14(i)=feature14_data
end

This can be used to get desired output:
cellfun(#(n) ~isempty(n), strfind(B, A{1}))

You could use ismember iteratively:
cellfun(#(x)ismember('#',x), B)

Related

Does not string.gmatch() return nil in Lua?

local str = ",23,4"
local t = {}
local i = 1
for temp in str:gmatch("[^,]+") do
t[i] = temp
i = i + 1
end
I'm a Lua newbie. Here is my code. I expected that t[1] has nil. However, gmatch() skipped it instead of returning nil. Tabel t[] has only two key-values. If I make table t[] like this
t[1] = nil
t[2] = 23
t[3] = 4
, how do I use gmatch()? Or what function do I have to use?
gmatch() didn't skip anything; it did exactly what you told it to: it found every occurrance of "[^,]+", of which there are two, and handed each of them to the loop body.
If you want to match empty strings as well, you can change your pattern to "[^,]*".
+ matches one or more
* matches zero or more
Please refer to https://www.lua.org/manual/5.3/manual.html#6.4.1

Issue with ASCii in Python3

I am trying to convert a string of varchar to ascii. Then i'm trying to make it so any number that's not 3 digits has a 0 in front of it. then i'm trying to add a 1 to the very beginning of the string and then i'm trying to make it a large number that I can apply math to it.
I've tried a lot of different coding techniques. The closest I've gotten is below:
s = 'Ak'
for c in s:
mgk = (''.join(str(ord(c)) for c in s))
num = [mgk]
var = 1
num.insert(0, var)
mgc = lambda num: int(''.join(str(i) for i in num))
num = mgc(num)
print(num)
With this code I get the output: 165107
It's almost doing exactly what I need to do but it's taking out the 0 from the ord(A) which is 65. I want it to be 165. everything else seems to be working great. I'm using '%03d'% to insert the 0.
How I want it to work is:
Get the ord() value from a string of numbers and letters.
if the ord() value is less than 100 (ex: A = 65, add a 0 to make it a 3 digit number)
take the ord() values and combine them into 1 number. 0 needs to stay in from of 65. then add a one to the list. so basically the output will look like:
1065107
I want to make sure I can take that number and apply math to it.
I have this code too:
s = 'Ak'
for c in s:
s = ord(c)
s = '%03d'%s
mgk = (''.join(str(s)))
s = [mgk]
var = 1
s.insert(0, var)
mgc = lambda s: int(''.join(str(i) for i in s))
s = mgc(s)
print(s)
but then it counts each letter as its own element and it will not combine them and I only want the one in front of the very first number.
When the number is converted to an integer, it
Is this what you want? I am kinda confused:
a = 'Ak'
result = '1' + ''.join(str(f'{ord(char):03d}') for char in a)
print(result) # 1065107
# to make it a number just do:
my_int = int(result)

Given two strings, how do I find number of reoccurences of one in another?

For example, s1='abc', s2='kokoabckokabckoab'.
Output should be 3. (number of times s1 appears in s2).
Not allowed to use for or strfind. Can only use reshape,repmat,size.
I thought of reshaping s2, so it would contain all of the possible strings of 3s:
s2 =
kok
oko
koa
oab
.... etc
But I'm having troubles from here..
Assuming you have your matrix reshaped into the format you have in your post, you can replicate s1 and stack the string such that it has as many rows as there are in the reshaped s2 matrix, then do an equality operator. Rows that consist of all 1s means that we have found a match and so you would simply search for those rows where the total sum is equal to the total length of s1. Referring back to my post on dividing up a string into overlapping substrings, we can decompose your string into what you have posted in your question like so:
%// Define s1 and s2 here
s1 = 'abc';
len = length(s1);
s2 = 'kokoabckokabckoab';
%// Hankel starts here
c = (1 : len).';
r = (len : length(s2)).';
nr = length(r);
nc = length(c);
x = [ c; r((2:nr)') ]; %-- build vector of user data
cidx = (1:nc)';
ridx = 0:(nr-1);
H = cidx(:,ones(nr,1)) + ridx(ones(nc,1),:); % Hankel subscripts
ind = x(H); % actual data
%// End Hankel script
%// Now get our data
subseqs = s2(ind.');
%// Case where string length is 1
if len == 1
subseqs = subseqs.';
end
subseqs contains the matrix of overlapping characters that you have alluded to in your post. You've noticed a small bug where if the length of the string is 1, then the algorithm won't work. You need to make sure that the reshaped substring matrix consists of a single column vector. If we ran the above code without checking the length of s1, we would get a row vector, and so simply transpose the result if this is the case.
Now, simply replicate s1 for as many times as we have rows in subseqs so that all of these strings get stacked into a 2D matrix. After, do an equality operator.
eqs = subseqs == repmat(s1, size(subseqs,1), 1);
Now, find the column-wise sum and see which elements are equal to the length of your string. This will produce a single column vector where 1 indicates that we have found a match, and zero otherwise:
sum(eqs, 2) == len
ans =
0
0
0
0
1
0
0
0
0
0
1
0
0
0
0
Finally, to add up how many times the substring matched, you just have to add up all elements in this vector:
out = sum(sum(eqs, 2) == len)
out =
2
As such, we have two instances where abc is found in your string.
Here is another one,
s1='abc';
s2='bkcokbacaabcsoabckokabckoabc';
[a,b] = ismember(s2,s1);
b = [0 0 b 0 0];
a1=circshift(b,[0 -1]);
a2=circshift(b,[0 -2]);
sum((b==1)&(a1==2)&(a2==3))
It gives 3 for your input and 4 for my example, and it seems to work well if ismember is okey.
Just for the fun of it: this can be done with nlfilter from the Image Processing Toolbox (I just discovered this function today and am eager to apply it!):
ds1 = double(s1);
ds2 = double(s2);
result = sum(nlfilter(ds2, [1 numel(ds1)], #(x) all(x==ds1)));

Is it possible to concatenate a string with series of number?

I have a string (eg. 'STA') and I want to make a cell array that will be a concatenation of my sting with a numbers from 1 to X.
I want the code to do something like the fore loop here below:
for i = 1:Num
a = [{a} {strcat('STA',num2str(i))}]
end
I want the end results to be in the form of {<1xNum cell>}
a = 'STA1' 'STA2' 'STA3' ...
(I want to set this to a uitable in the ColumnFormat array)
ColumnFormat = {{a},... % 1
'numeric',... % 2
'numeric'}; % 3
I'm not sure about starting with STA1, but this should get you a list that starts with STA (from which I guess you could remove the first entry).
N = 5;
[X{1:N+1}] = deal('STA');
a = genvarname(X);
a = a(2:end);
You can do it with combination of NUM2STR (converts numbers to strings), CELLSTR (converts strings to cell array), STRTRIM (removes extra spaces)and STRCAT (combines with another string) functions.
You need (:) to make sure the numeric vector is column.
x = 1:Num;
a = strcat( 'STA', strtrim( cellstr( num2str(x(:)) ) ) );
As an alternative for matrix with more dimensions I have this helper function:
function c = num2cellstr(xx, varargin)
%Converts matrix of numeric data to cell array of strings
c = cellfun(#(x) num2str(x,varargin{:}), num2cell(xx), 'UniformOutput', false);
Try this:
N = 10;
a = cell(1,N);
for i = 1:N
a(i) = {['STA',num2str(i)]};
end

Finding a specific character in a string in Matlab

Suppose I have a string 'johndoe#hotmail.com'. I want to store the string before and after "#" into 2 separate strings. What would be the easiest method of finding the "#" character or other characters in the string?
STRTOK and an index operation should do the trick:
str = 'johndoe#hotmail.com';
[name,address] = strtok(str,'#');
address = address(2:end);
Or the last line could also be:
address(1) = '';
You can use strread:
str = 'johndoe#hotmail.com';
[a b] = strread(str, '%s %s', 'delimiter','#')
a =
'johndoe'
b =
'hotmail.com'
For "easiest",
>> email = 'johndoe#hotmail.com'
email =
johndoe#hotmail.com
>> email == '#'
ans =
Columns 1 through 13
0 0 0 0 0 0 0 1 0 0 0 0 0
Columns 14 through 19
0 0 0 0 0 0
>> at = find(email == '#')
at =
8
>> email(1:at-1)
ans =
johndoe
>> email(at+1:end)
ans =
hotmail.com
It would be slightly more complicated if you were looking for something with more than one character, or you weren't sure if there was exactly one #, and in that case MATLAB has a lot of functions for searching through text, including regular expressions (see doc regexp).
TEXTSCAN works too.
str = 'johndoe#hotmail.com';
parts = textscan(str, '%s %s', 'Delimiter', '#');
returns a cell array where parts{1} is 'johndoe' and parts{2} is 'hotmail.com'.
If this thread isn't completely enumerated by now, may I add another? A handy perl-based MATLAB function:
email = 'johndoe#hotmail.com';
parts = regexp(email,'#', 'split');
parts is a two element cell array similar to mtrw's implementation of textscan. Maybe overkill, but regexp is much more useful when splitting a string by multiple delimiting characters or pattern searching. The only downside is the use of regular expressions which I still haven't mastered after 15 years of coding.
I used strtok and strrep from Matlab instead.
String email = "johndoe#hotmail.com";
String a[] = email.split("#");
String def = null;
String ghi = null;
for(int i=0;i<a.length;i++){
def = a[0];
ghi = a[1];
}

Resources