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.
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 have a question regarding indexing and loops in MATLAB. I have a vector of length n (named data in the code below). I want to examine this vector 4 elements at a time inside of a for loop. How can I do this? My attempt included below does not work because it will exceed the array dimensions at the end of the loop.
for k = 1:length(data)
index = k:k+3;
cur_data = data(index);
pre_q_data1 = cur_data(1);
pre_q_data2 = cur_data(2);
% Interweaving the data
q = [pre_q_data1; pre_q_data2];
qdata = q(:)';
pre_i_data1 = cur_data(3);
pre_i_data2 = cur_data(4);
i = [pre_i_data1; pre_i_data2];
idata = i(:)';
end
You shouldn't have k go all the way to length(data) if you're planning on indexing up to k+3.
I've also taken the liberty of greatly simplifying your code, but feel free to ignore that!
for k = 1:length(data)-3
% maximum k = length(data)-3, so maximum index = length(data)-3+3=length(data)
index = k:k+3;
cur_data = data(k:k+3);
% Interweaving the data
q = cur_data(1:2); % transpose at end of line here if need be
i = cur_data(3:4); % could just use data(k+2:k+3) and not use cur_data
end
I'm doing a set of problems from the MATLAB's introductory course at MIT OCW. You can see it here, it's problem number 9, part g.iii.
I have one matrix with the final grades of a course, all of them range from 1 to 5. And I have another array with only letters from 'F' to 'A' (in a 'decreasing' order).
I know how to change elements in a matrix, I suppose I could do something like this for each number:
totalGrades(find(totalGrades==1)) = 'F';
totalGrades(find(totalGrades==2)) = 'E';
totalGrades(find(totalGrades==3)) = 'C';
totalGrades(find(totalGrades==4)) = 'B';
totalGrades(find(totalGrades==5)) = 'A';
But then, what's the purpose of creating the string array "letters"?
I thought about using a loop, but we're supposed to solve the problem without one at that point of the course.
Is there a way? I'll be glad to know. Here's my code for the whole problem, but I got stuck in that last question.
load('classGrades.mat');
disp(namesAndGrades(1:5,1:8));
grades = namesAndGrades(1:15,2:size(namesAndGrades,2));
mean(grades);
meanGrades = nanmean(grades);
meanMatrix = ones(15,1)*meanGrades;
curvedGrades = 3.5*(grades./meanMatrix);
% Verifying
nanmean(curvedGrades)
mean(curvedGrades)
curvedGrades(curvedGrades>=5) = 5;
totalGrades = nanmean(curvedGrades,2);
letters = 'FDCBA';
Thanks a lot!
Try:
letters=['F','D','C','B','A'];
tg = [1 2 1 3 3 1];
letters(tg)
Result:
ans = FDFCCF
This works even when tg (total grade) is a matrix:
letters=['F','D','C','B','A'];
tg = [1 2 1 ; 3 3 1];
result = letters(tg);
result
result =
FDF
CCF
Edit (brief explanation):
It is easy to understand that when you do letters(2) you get the second element of letters (D).
But you can also select several elements from letters by giving it an array: letters([1 2]) will return the first and second elements (FD).
So, letters(indexesArray) will result in a new array that has the same length of indexesArray. But, this array has to contain numbers from 1 to the length of letters (or an error will pop up).
I have an array of arrays. I'm trying to go through each array in my main array and compare the index's against variables. If they are larger than the variables I want to delete that array from the parent array. At the minute when I run the below code when comparing against b, it works the first time and deletes the array but doesn't seem to run again and delete the rest of the arrays where index > b. Any help appreciated.
The following function should print 14 but currently it's printing 16.
a = 4
b = 2
c = 5
n = 5
def count_configurations(a, b, c, n)
a = (0..a).to_a
b = (0..b).to_a
c = (0..c).to_a
big = [a.max, b.max, c.max].max
big = (0..big).to_a
arrays = big.repeated_permutation(3).to_a
solutions = []
arrays.each do |array|
sum = 0
array.each { |a| sum+=a }
if sum == n
solutions << array
end
end
solutions = solutions.uniq
solutions.each do |solution|
if solution[0] > a.max
solutions.delete(solution)
end
end
solutions.each do |solution|
if solution[1] > b.max
solutions.delete(solution)
end
end
solutions.each do |solution|
if solution[2] > c.max
solutions.delete(solution)
end
end
puts solutions.count
end
Assuming solutions contains the proper array, here you go, instead of three each loops:
solutions = solutions.uniq.reject do |solution|
solution[0] > a.max || solution[1] > b.max || solution[2] > c.max
end
Details: Enumerable#reject.
Alrighty, so I have a pretty 'simple' problem on my hands. I am given two inputs for my function: a string that gives the formula of the equation and a structure that contains the information I need and looks like this:
Name
Symbol
AtomicNumber
AtomicWeight
To find the molecular weight, I have to take all of the elements in the formula, find their total mass and add them all together. For example, let's say that I have to find the molecular weight of oxygen. The formula would look like:
H2,O
The molecular weight will thus be
2*(Hydrogen's weight) + (Oxygen's weight), which evaluates to 18.015.
There will always be a comma separating the different elements in a formula. What I am having trouble with right now, is taking the number out of the string(the formula). I feel like I'm over-complicating how I am going about extracting it. If there's a number, I know it can be in positions 2 or 3 (depending on the element name). I tried to use isnumeric, I tried to do some really weird, coding stuff (which you'll see below), but I am having difficulties.
test case:
mass5 = molarMass('C,H2,Br,C,H2,Br', table)
mass5 => 187.862
table:
Name Symbol AtomicNumber AtomicWeight
'Carbon' 'C' 6 12.0110000000000
'Hydrogen' 'H' 1 1.00800000000000
'Nitrogen' 'N' 7 14.0070000000000
'Oxygen' 'O' 8 15.9990000000000
'Phosphorus''P' 15 30.9737619980000
'Sulfur' 'S' 16 32.0600000000000
'Chlorine' 'Cl' 17 35.4500000000000
'Bromine' 'Br' 35 79.9040000000000
'Sodium' 'Na' 11 22.9897692800000
'Magnesium' 'Mg' 12 24.3050000000000
My code so far is:
function[molar_mass] = molarMass(formula, information)
Names = []; %// Creates a Name array
[~,c] = size(information); %Finds the rows and columns of the table
for i = 1:c %Reads through the columns
Molecules = getfield(information(:,i), 'Name'); %Finds the numbers in the 'Name' area
Names = [Names {Molecules}];
end
Symbols = [];
[~, c2] = size(information);
for i = 1:c2 %Reads through the columns
Symbs = getfield(information(:,i), 'Symbol'); %Finds the numbers in the 'Symbol'
Symbols = [Symbols {Symbs}];
end
AN = [];
[~, c3] = size(information);
for i = 1:c3 %Reads through the columns
Atom = getfield(information(:,i), 'AtomicNumber'); %Finds the numbers in the 'AtomicWeight' area
AN = [AN {Atom}];
end
Wt = [information(:).AtomicWeight];
formula_parts = strsplit(formula, ','); % cell array of strings
total_mass = 0;
multi = [];
atoms = [];
Indices = [];
for ipart = 1:length(formula_parts)
part = formula_parts{ipart}; % Takes in the string
isdigit = (part >= '0') & (part <= '9'); % A boolean array
atom = part(~isdigit); % Select all chars that are not digits
Indixes = find(strcmp(Symbols, atom));
Indices = [Indices {Indixes}];
mole = atom;
atoms = [atoms {mole}];
natoms = part(isdigit); % Select all chars that are digits
% Convert natoms string to numbers, default to 1 if missing
if length(natoms) == 0
natoms = '1';
multi = [multi {natoms}];
else
natoms = num2str(natoms);
multi = [multi {natoms}];
end
end
multi = char(multi);
multi = str2num(multi); %Creates a number array with my multipliers
f=56;
Molecule_Wt = Wt{Indices};
duck = 62;
total_mass = total_mass + atom_weight * multi;
end
Thanks to Bas Swinckels I can now extract the numbers from the formulas, but what I'm struggling with now is how to pull out the weights associated with the symbols. I created my own weight_chart, but strcmp won't work there. Neither will strfind or strmatch. What I want to do is find the formulas in my input, in the chart. Then index it from that index, to the column (so add 1 I believe). How do I find the indices though? I'd prefer to find them in the order the strings appear in my input, since I can then apply my 'multi' array to it.
Any help/suggestions would be appreciated :)
Given the string, you can pull out the part that is a digit character with the isstrprop function. Then use that to address your string to get just those characters, then cast that as a double with str2double.
PartialString = 'H12';
Subscript = str2double (PartialString (isstrprop (PartialString, 'digit')));
This should get you started, there is still some parts that need to be filled in:
formula_parts = strsplit(formula, ','); % cell array of strings
total_mass = 0;
for ipart = 1:length(formula_parts)
part = formula_parts{ipart}; % string like 'H2'
isdigit = isstrprop(part, 'digit'); % boolean array
atom = part(~isdigit); % select all chars that are not digits
natoms = part(isdigit); % select all chars that are digits
% convert natoms string to int, default to 1 if missing
if length(natoms) == 0
natoms = 1;
else
natoms = num2str(natoms);
end
% calculate weight
atom_weight = lookup_weight(atom); % somehow look up value in table
total_mass = total_mass + atom_weight * natoms;
end
See this old question about how to extract letters or digits from a string.