Text input example:
"The exponent of [i]*[i]*[i]*0,5 is [i*i*i*0.5]"|%d,%f,%d,%e
Text output must be:
"The exponent of [%d]*[%f]*[%d]*0,5 is [%e]"|i,i,i,i*i*i*0.5
What I did:
I tried first to put them in two arrays
array_text ['i', 'i', 'i', 'i*i*i*0.5']
array_parameters ['%d', '%f', '%d', '%e']
But I don't know how to switch array_text[0] in the text with array_parameters[0],
array_text[1] in the text with array_parameters[1] and so on with the other array items.
How can I swap them in order to realize my text output as indicated above?
Edit:
I tried to resolve it like this:
let end = 0
for i in range(0,len(array_text)-1)
let idx = match(textstring, '\[\zs'.array_text[i], end)
let end = matchend(textstring, array_text[i], idx)
if end > idx
let text = substitute(text, strpart(text, idx, end-idx), array_parameters[i], '')
endif
endfor
That seems to work but the substitute/strpart command doesn't work.
Related
I have 60 different character arrays loaded in my workspace (Book01, Book02, ..., Book60). For example, Book01 is a 1x202040 char.
I'm working in a script file, and trying to separate the last sentence("RandomInfoAtEnd") of Book45 until Book58:
WholeBook = Book50; % Call Array for test
for i = 1:60
book = eval(['Book' num2str(i)]);
if i >= 45 && i <= 58
% Procedure to separate last sentence.
Chr = convertStringsToChars(WholeBook);
SearchedUnit = '.!?' ; % Sentence end punctuation
idx = ismember (Chr, SearchedUnit);
Loc = find (idx, 2, 'last'); % Find second last sentence-ending-punctuation
if numel (Loc) < 2
error ('the requested character cannot be found')
end
SecondLastLocation = Loc (1);
AllLocations = find (idx);
RandomInfoAtEnd = extractAfter(WholeBook,SecondLastLocation);
else
RandomInfoAtEnd = ''; % No sentence separated
end
end
Right now I have a problem only with the IF-statement or FOR-loop logic, in such a way that RandomInfoAtEnd = '' for any array that is called.
My procedure is working fine, as it separated the last sentence perfectly from any array, but what am I doing wrong with the FOR-loop/IF-statement?
Thanks.
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 have many 33213168x1 cell arrays, where each cell contains an 85 x 1 column.
Each cell in the column is in the form
[0.55;0.25;0.75]
[0.33;0.66;0.99]
I want to split up this single column by the semi-colon delimiter so that each cell in the cell array is 85x3, like:
[0.55][0.25][0.75]
[0.33][0.66][0.99]
I've tried numerous techniques to solve this, but most commonly get the errors 'cell elements must be character arrays' or 'input must be a string.'
Some of the approaches I've tried:
splitcells = strsplit(regress_original_053108,';');
splitcells = cellfun(#(x) strsplit(regress_original_053108, ';'),regress_original_053108 , 'UniformOutput',0);
splitcells = regexp(regress_original_053108, ';', 'split');
splitcells = textscan(regress_original_053108, 'delimiter', ';');
Etc. Any feedback about how to do this would be appreciated.
Hope this solves your problem:
% Example input
input = {[0.55;0.25;0.75]};
cellArray(1:85,1) = input;
% Create array
doubleArray = zeros(85,3);
% Fill array
for i=1:85
doubleArray(i,:) = cellArray{i,1}';
end
Each cell you have is not a string, hence you can't use strsplit. Use this approach:
for ii = length(X) % Here X denotes your 33213168x1 cell array
X{ii} = cell2mat(cellfun(#(y) y.', X{ii}, 'UniformOutput', false));
end
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 am trying to get the legend to show a fixed value and one that is from an array. I have managed to get the fixed value to display and when I manually enter the position from the array this will display. I want the position selected from the array to advance by 1 each time. I tried to use the n variable that I have defined in the script but it doesn't seem to work. At the moment I have entered a value of 4 and this selects the 4th value from the array. I am new to matlab and can't for the life of me think how to do this. Any help is appreciated. Here is my script I am working with.
clear
clc
f = #(x,k,lamda) ((lamda.^k).*(x.^(k-1)).*(exp(-lamda.*x))./(factorial(k-1)));
colors = ['k', 'r' , 'g', 'b', 'y', 'm', 'c'];
hold on
n=1;
k = 5;
x = 0 :0.1: 10;
for lamda = 1 : 0.2 : 2;
ncol= mod(n,7)+1;
plot(x,f(x,k,lamda), 'Color', colors(ncol))
l = 1 : 0.2 : 2;
legstr(n,:) = strcat ('k = ', num2str(k), ' Lamda = ', num2str (l(4)));
legend(legstr)
title('Erlang Distribution')
xlabel('X')
ylabel('f(x,k,lamda)')
n=n+1;
end
hold off
You were almost there. No need to introduce another vector to pull from since the value you have assigned to lamda in each loop iteration will work. Also you'll have to change legstr from a matrix to a cell array to deal with the fact that lamda sometimes has an extra digit in the decimal place, which will cause the legend string to be longer or shorter, depending.
clear all
clc
f = #(x,k,lamda) ((lamda.^k).*(x.^(k-1)).*(exp(-lamda.*x))./(factorial(k-1)));
colors = ['k', 'r' , 'g', 'b', 'y', 'm', 'c'];
hold on
n=1;
k = 5;
x = 0 :0.1: 10;
for lamda = 1 : 0.2 : 2;
ncol= mod(n,7)+1;
plot(x,f(x,k,lamda), 'Color', colors(ncol))
% l = 1 : 0.2 : 2;
legstr{n} = strcat ('k = ', num2str(k), ' Lamda = ', num2str (lamda));
legend(legstr)
title('Erlang Distribution')
xlabel('X')
ylabel('f(x,k,lamda)')
n=n+1;
end
hold off