Replacing string with empty vector - matlab

I'm trying to modify this code so that if the input of this function contains the letter 'Z', it will return return an empty vector. I am able to do this for the letter 'Q' or 'Z' if it is at the beginning of string, but unfortunately it won't work if either of these two letters are at the end.
function d = change(a)
new_claim = regexprep(a, 'A', '2');
new_claim1 = regexprep(new_claim, 'B', '2');
new_claim2 = regexprep(new_claim1, 'C', '2');
new_claim3 = regexprep(new_claim2, 'D', '3');
new_claim4 = regexprep(new_claim3, 'E', '3');
new_claim5 = regexprep(new_claim4, 'F', '3');
new_claim6 = regexprep(new_claim5, 'G', '4');
new_claim7 = regexprep(new_claim6, 'H', '4');
new_claim8 = regexprep(new_claim7, 'I', '4');
new_claim9 = regexprep(new_claim8, 'J', '5');
new_claim10 = regexprep(new_claim9, 'K', '5');
new_claim11 = regexprep(new_claim10, 'L', '5');
new_claim12 = regexprep(new_claim11, 'M', '6');
new_claim13 = regexprep(new_claim12, 'N', '6');
new_claim14 = regexprep(new_claim13, 'O', '6');
new_claim15 = regexprep(new_claim14, 'P', '7');
new_claim16 = regexprep(new_claim15, 'R', '7');
new_claim17 = regexprep(new_claim16, 'S', '7');
new_claim18 = regexprep(new_claim17, 'T', '8');
new_claim19 = regexprep(new_claim18, 'U', '8');
new_claim20 = regexprep(new_claim19, 'V', '8');
new_claim21 = regexprep(new_claim20, 'W', '9');
new_claim22 = regexprep(new_claim21, 'X', '9');
new_claim23 = regexprep(new_claim22, 'Y', '9');
new_claim24 = regexprep(new_claim23, '-', ' ');
new_claim25 = regexprep(new_claim24, '(', '');
new_claim26 = regexprep(new_claim25, ')','');
d = new_claim26;
if strfind(d,'Q') == true
d = [];
elseif strfind(d,'Z') == true
d = [];
else
return;
end

If it's your desire to check to see if a string contains the letter Z or z, maybe put this at the beginning of your code:
if ~isempty(regexp(a, '[Zz]'))
d = [];
return;
end
If you also wanted to check for Q or q, you can do:
if ~isempty(regexp(a, '[ZzQq]'))
d = [];
return;
end
The above uses a regular expression to see if there are any characters in your string that contain either Z or z (or Q or q, depending on what you want). regexp returns the indices of where these characters were found. If there were Z or z (or Q or q characters, depending on what you want) characters found, then the indices would be non-empty, hence the ~isempty check. If there were no Z or z (or Q or q) characters that were found, this would be empty and so this statement is skipped. What's important is that if we have found Z, z (or Q, q) characters, we immediately make d empty and return so that the rest of the logic is not run.
You can then carry on with the rest of your code.

You can check if a character is in a string with: any(d == 'Q') || any(d == 'Z')

Related

LateInitializationError: Local 'res' has not been initialized

I am making a program for encrypting text relative to a keyword. Initially, the algorithm was written in Python, everything worked correctly. I decided to translate it into a mobile application in Flutter, so I had to rewrite it in Dart.
List symbols = ['a', 'b', 'c', 'd', 'e',
'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', ',', ';', ':', '-',
'_', ' ', '!', '#', '#', '\$', '%', '^', '&', '*', '(', ')', '+', '=', '\"', '№', '~', '?',
'\\', '/', '|', '[', ']', '{', '}', '`', '\'', '<', '>'];
late dynamic keyWord = ' ';
late dynamic text = ' ';
late dynamic res;
late dynamic a4;
late dynamic m;
late dynamic n;
late dynamic f;
var d = 0;
var k = 0;
var z = 0;
var operation = 0;
var m1 = 0;
var c = 0;
encode(keyWord, text){
late dynamic res;
var l = (text.length) as int;
for( var i = l ; i >= 1; i-- ){
if(symbols.contains(text[d])) {
var f = symbols.indexOf(text[d]);
a4 = '';
if(f == 0){
a4 = '0';
while(f > 0){
a4 = (f % 4).toString() + a4.toString();
f = f ~/ 4;
}
a4 = '$a4';
}
}
else{
a4 += '1123';
}
while(a4.length != 4) {
a4 = '0' + a4;
}
for( var j = 4 ; j >= 1; j-- ){
res += (keyWord[(a4[z]) as int]);
res.whenComplete((){
setState(() {});
});
z += 1;
}
z = 0;
d += 1;
}
return(res);
}
The encode function does not work due to LateInitializationError: Local 'res' has not been initialized.
The rest of the code works correctly, the res
variable was not called anywhere except for this function. Maybe someone faced the same problem or knows how to solve it? I would be grateful for your help.
Exception is pretty self-explanatory. You access res in res += (keyWord[(a4[z]) as int]);, when it's not yet initialized. You should remove late and do dynamic res = '' for example.
P.S. 'res += value' is basically 'res = res + value'. That's why you get the error
P.S.S Why not to use some typing? Your code's res.whenComplete will fail, because it's not a Future. Seems, that you don't use any code completion at all.

Text Corrector Function

I'm trying to make a stringCorrector function for avoiding non-English words, but don't know how to implement this to string in Dart;
as an example;
İstanbul ====> Istanbul, New York===> NewYork, İşÇöÜ===>IsCoU
String stringCorrector(String string) {
Map<String, String> correcterMap = {
'ö': 'o',
'ü': 'u',
'Ö': 'O',
'Ü': 'U',
'İ': 'I',
'ı': 'i',
'ğ': 'g',
'Ğ': 'G',
' ': ''
};
}
May be I can help you if I understand the use of this. For the first two examples, it is relatively easy, but for the third one you will need to have a whole map of values to replace.
You would get something like this :
String stringCorrector(String string) {
Map<String, String> correcterMap = {
'ö': 'o',
'ü': 'u',
'Ö': 'O',
'Ü': 'U',
'İ': 'I',
'ı': 'i',
'ğ': 'g',
'Ğ': 'G',
' ': '',
};
correcterMap.forEach((key, value) {
string = string.replaceAll(RegExp(key), value);
});
return string;}
It's just a matter of mapping the string and reducing that result.
String stringCorrector(String string) {
Map<String, String> correcterMap = {
'ö': 'o',
'ü': 'u',
'Ö': 'O',
'Ü': 'U',
'İ': 'I',
'ı': 'i',
'ğ': 'g',
'Ğ': 'G',
' ': ''
};
return string.runes.map(
(int charCode) {
String char = String.fromCharCode(charCode);
return correcterMap[char] ?? char;
}
).reduce((a, b) => a + b);
}

I am getting Range error like Invalid value: Not in range 16..17, inclusive: 19Error: RangeError (end):

I am using Dart pad for to show the list items using sublist but i am getting range error here is my code please help me.
void main() {
var list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q','f','e','y'];
for(int i=0;i<list.length;i++) {
if(i.isEven && i<list.length-1)
print(list.sublist(i,i+2));
else
print(list.sublist(i,i+3));
}
}
You tried accessing some items outside the list with i + 3. Using i<list.length-2 will allow the compiler to check if the item is in the list before trying to print the value.
void main() {
var list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q'];
for(int i=0;i<list.length;i++) {
if(i.isEven && i<list.length-2)
print(list.sublist(i,i+2));
else if(i<list.length-2)
print(list.sublist(i, i+3));
}
}

Matlab subsref and end

I have a custom matrix class, that I want to be able to index as:
x = myobj(1,2).d(3,4) % myobj(1,2,3,4)
x = myobj(2, 3).d(3, end) % myobj(1,3,1,end)
I want these to work for assignment too.
I've started with:
class MyClass < double
methods
function obj = MyClass(x)
obj = obj#double(x);
end
function obj = subsref(obj, s)
varargout{:} = subsref#double(obj, subsintercept(obj, s));
end
function obj = subsasgn(obj, s, b)
obj = subsasgn#double(obj, subsintercept(obj, s), b);
end
end
end
And then I can mess with the indexing in subsintercept. However, I've hit a problem. With a minimal implementation:
function s = subsintercept(obj, s)
disp('subsintercept');
for i = 1:length(s)
disp(s(i));
end
end
I get this expected behaviour
>> myobj = MyClass(zeros(1,2,3,4))
>> myobj(1,2).d(3,4)
subsintercept
type: '()'
subs: {[1] [2]}
type: '.'
subs: 'd'
type: '()'
subs: {[3] [4]}
<error due to not having finished subsintercept yet>
But this unexpected
>> myobj(1,2).d(3,end)
subsintercept
type: '()'
subs: {[1] [2]}
type: '.'
subs: 'd'
<error due to not having finished subsintercept yet>
Why does adding the end cause me not to receive the 3?
Is this behaviour documented?
Some testing reveals that this:
result = myobj(1,2).d(3,end)
Is translated to:
end_ = str2func('end');
d_temp = subsref(myobj, substruct('()', {1 2}, '.', 'd'));
ei = end_(d_temp, 2, 2);
result = subsref(myobj, substruct('()', {1 2}, '.', 'd', '()', {3, ei}));
i.e., that subsref gets called twice!*
*And surprisingly, not as subsref(d_temp, substruct('()', {3, ei}))the second time

removing duplicates - ** only when the duplicates occur in sequence

I would like to do something similar to the following, except I would only like to remove 'g' and'g' because they are the duplicates that occur one after each other. I would also like to keep the sequence the same.
Any help would be appreciated!!!
I have this cell array in MATLAB:
y = { 'd' 'f' 'a' 'g' 'g' 'w' 'a' 'h'}
ans =
'd' 'f' 'a' 'w' 'a' 'h'
There was an error in my first answer (below) when used on multiple duplicates (thanks grantnz). Here's an updated version:
>> y = { 'd' 'f' 'a' 'g' 'g' 'w' 'a' 'h' 'h' 'i' 'i' 'j'};
>> i = find(diff(char(y)) == 0);
>> y([i; i+1]) = []
y =
'd' 'f' 'a' 'w' 'a' 'j'
OLD ANSWER
If your "cell vector" always contains only single character elements you can do the following:
>> y = { 'd' 'f' 'a' 'g' 'g' 'w' 'a' 'h'}
y =
'd' 'f' 'a' 'g' 'g' 'w' 'a' 'h'
>> y(find(diff(char(y)) == 0) + [0 1]) = []
y =
'd' 'f' 'a' 'w' 'a' 'h'
Look at it like this: you want to keep an element if and only if either (1) it's the first element or (2) its predecessor is different from it and either (3) it's the last element or (4) its successor is different from it. So:
y([true ~strcmp(y(1:(end-1)),y(2:end))] & [~strcmp(y(1:(end-1)),y(2:end)) true])
or, perhaps better,
different = ~strcmp(y(1:(end-1)),y(2:end));
result = y([true different] & [different true]);
This should work:
y([ diff([y{:}]) ~= 0 true])
or slightly more compactly
y(diff([y{:}]) == 0) = []
Correction : The above wont remove both the duplicates
ind = diff([y{:}]) == 0;
y([ind 0] | [0 ind]) = []
BTW, this works even if there are multiple duplicate sequences
eg,
y = { 'd' 'f' 'a' 'g' 'g' 'w' 'a' 'h' 'h'};
ind = diff([y{:}]) == 0;
y([ind 0] | [0 ind]) = []
y =
'd' 'f' 'a' 'w' 'a'