I created an array of strings and I want to get the value of a given string at a given position, but the returned value is the character and not the string, eg:
myArray = ['string1' 'string2' 'string3'];
s = myArray(1); //returns the character at the position 1, instead of the string
How can I get the value of these strings based on a given position i ?
Try using a cell array:
myArray = {'string1' 'string2' 'string3'};
s = myArray{1};
You can do a for loop if this is what you are asking for.
myArray=['b' 'c' 'd']
for i =1:lenght(myArray)
s(i)=myArray(i);
end
Not sure what you are asking for exactly.
Related
I am trying to write a program that returns the length of longest substring within a string.
This is my code:
def lengthOfLongestSubstring():
dict = {}
s = 'dvdf'
max_substr_length = 0
max_substr = ''
if len(s) < 1:
return 0
else:
for letter in s:
print('String value: ', s)
if letter not in max_substr:
max_substr = max_substr + letter
max_substr_length = len(max_substr)
dict[max_substr] = dict.get(max_substr, max_substr_length)
print(letter, max_substr, max_substr_length, dict)
elif letter in max_substr:
dict[max_substr] = dict.get(max_substr, max_substr_length)
s = s[s.index(letter)+1:]
max_substr = ''
max_substr_length = 0
print(s, letter, max_substr, max_substr_length, dict)
print(dict)
print(max(dict.values(), default=0))
For the input string s = 'dvdf'
I am getting rid of the first instance of the letter that gets repeated in the input string s, in line 18 of my code s = s[s.index(letter)+1:].
So when the second 'd' is encountered, s should get updated to s = 'vdf'
Unfortunately, the for loop doesn't start iterating from the 0th index of this new s. Is there a way that doesn't involve iterating over integer indexes to get the for loop to begin iterating from the beginning when the string is updated?
Well, no not this way.
Python iterates whatever was s in the beginning of the loop.
You should try a different approach like using a cellar storage.
Push every letter to it in the correct order,
loop untils its empty,
pop a value,
do whatever you want with it,
push a value to it, if necessary.
in the end you should have a working example.
I'm trying to add a string to a "cell" type value of matlab. I want to add it as the first value of the "array" variable, without deleting the actual first value.
What I have:
array = {'A001','A002','B001','B002','B003','B004','C001','C004'}
add_to_start = 'Time'
What I want:
array = {'Time','A001','A002','B001','B002','B003','B004','C001','C004'}
EDIT
Actually my input array is:
array = {'A001','A002','B001','B002','B003','B004','C001';'','','','','','',''}
How can I delete the empty row?
You can use an array to add an element to your cell:
mycell = ['time',mycell];
Or using the colon operator:
mycell = {'time',mycell{:}};
I am trying to use the numeric charecters from the array held within the positions argument as indices to access the characters of the string inside the string argument to subsequently print a new string. I have an idea of what I need to do to get it to work, but I am hung up.
Total code thus far:
def scramble_string(string, positions)
str = string
pos = positions.join
newstr = []
i = 0
while i < pos.length do
return newstr.push(str[pos[i]])
i += 1
end
end
scramble_string("hello", [2, 3, 4, 5])
I suspect my problem lies within this part of the code...
return newstr.push(str[pos[i]])
If I understand you, you can use the following to get a given substring of a string, using a range:
'this is a string'[5..8]
=> "is a"
A simple way would be:
str = 'this is a string'
positions = [2,3,6,9,10]
new_str = positions.map {|p| str[p]}.join
=> "iss s"
str = 'this is a string'
positions = [2,3,6,9,1]
str.split('').values_at(*positions).join
#=> "iss h"
Another way, one that does not use join:
positions.each_with_object('') { |i,s| s << str[i] }
I have a cell array that contains a long list of strings. Most of the strings are in duplicates. I need the indices of instances of a string within the cell array.
I tried the following:
[bool,ind] = ismember(string,var);
Which consistently returns scalar ind while there are clearly more than one index for which the contents in the cell array matches string.
How can I have a list of indices that points to the locations in the cell array that contains string?
As an alternative to Divakar's comment, you could use strcmp. This works even if some cell doesn't contain a string:
>> strcmp('aaa', {'aaa', 'bb', 'aaa', 'c', 25, [1 2 3]})
ans =
1 0 1 0 0 0
Alternatively, you can ID each string and thus have representative numeric arrays corresponding to the input cell array and string. For IDing, you can use unique and then use find as you would with numeric arrays. Here's how you can achieve that -
var_ext = [var string]
[~,~,idx] = unique(var_ext)
out = find(idx(1:end-1)==idx(end))
Breakdown of the code:
var_ext = [var string]: Concatenate everything (string and var) into a single cell array, with the string ending up at the end (last element) of it.
[~,~,idx] = unique(var_ext): ID everything in that concatenated cell array.
find(idx(1:end-1)==idx(end)): idx(1:end-1) represents the numeric IDs for the cell array elements and idx(end) would be the ID for the string. Compare these IDs and use find to pick up the matching indices to give us the final output.
Sample run -
Inputs:
var = {'er','meh','nop','meh','ya','meh'}
string = 'meh'
Output:
out =
2
4
6
regexp would solve this problem better and the easy way.
string = ['my' 'bat' 'my' 'ball' 'my' 'score']
expression = ['my']
regexp(string,expresssion)
ans = 1 6 12
I have a string 'ADSL'. I want to find this string in an array of strings char('PSTN,ADSL','ADSL,VDSL','FTTH,VDSL')
when i run this command
strmatch('ADSL',char('PSTN,ADSL','ADSL,VDSL','FTTH,VDSL'));
the output is 2
But I expect the output as [1 2]
strmatch only gives positive result if the search string appears at the begining of row.
How can I find the search string if it occurs anywhere in the row?
Given the following input:
array = {'PSTN,ADSL', 'ADSL,VDSL', 'FTTH,VDSL'};
str = 'ADSL';
We find the starting position of each string match using:
>> pos = strfind(array, str)
pos =
[6] [1] []
or
>> pos = regexp(array, str)
pos =
[6] [1] []
We can then find the indices of matching strings using:
>> matches = find(~cellfun(#isempty,pos))
matches =
1 2
For an array of strings, it's better to use a cell array. That way strings can be of differnet lengths (and regexp can be applied on all cells at once):
cellArray = {'PSTN,ADSL','ADSL,VDSL','FTTH,VDSL'};
str = 'ADSL';
Then:
result = find(~cellfun('isempty', regexp(cellArray, str)));
will give what you want.
If you really have a char array as in your example,
array = char('PSTN,ADSL','ADSL,VDSL','FTTH,VDSL');
you can convert to a cell array (with cellstr) and apply the above:
result = find(~cellfun('isempty', regexp(cellstr(array), str)));
i would use strfind
a=strfind(cellstr(char('PSTN,ADSL','ADSL,VDSL','FTTH,VDSL')),'ADSL');
in this case will be a three by one cell array containing the index where you string starts at in the corresponding string