If statement with strings of letters in Matlab - matlab

I have in Matlab the following cells containing various combinations of the letters a,b,c,d
%all combinations containing 'a' and/or 'b'
G1={'a', 'ab', 'ac', 'ad', 'abc', 'acd', 'abd', 'abcd', 'b', 'bc', 'bd', 'bcd'};
%all combinations containing 'c' and/or 'd'
G2={'c', 'ac', 'bc', 'cd', 'abc', 'acd', 'bcd', 'abcd', 'd', 'ad', 'bd', 'abd'};
%all combinations containing 'c'
G3={'c', 'ac', 'bc', 'cd', 'acd', 'abd', 'bcd', 'abcd'};
I then construct a cell all of dimension
allsize=size(G1,2)*size(G2,2)*size(G3,2);
containing all possible ways to match one element of G1 with one element of G2 with one element of G3.
all=cell(allsize,3);
count=0;
for h=1:size(G1,2)
for k=1:size(G2,2);
for j=1:size(G3,2);
count=count+1;
all(count,1)=G1(h);
all(count,2)=G2(k);
all(count,3)=G3(j);
end
end
end
Question: I want to construct a vector check of dimension allsize x 1 such that check(l)=1 if [all(l,1) contains a and all(l,2) contains c] or [all(l,1) contains b and all(l,2) contains d], and zero otherwise.
I am having problems in writing the if condition
check=zeros(allsize,1);
for l=1:allsize
%if [all(l,1) contains a and all(l,2) contains c] or [all(l,1) contains b and all(l,2) contains d]
check(l)=1;
%end
end
Could you kindly provide some help?

(For the if statement, always best to show what you tried rather than some pseudo code , however...)
Firstly using all as a variable name is bad - it's an important built-in function and one you may want to use... I've renamed it allG below. But you probably want something like this:
check(l) = (any(allG{l,1}=='a') && any(allG{l,2}=='c')) || ...
(any(allG{l,1}=='b') && any(allG{l,2}=='d'))
Note I haven't used an if statement, since the right hand side evaluates to a logical value (a true/false value) which can be generally used in the same way as 1 and 0...
Also above we're treating the strings as arrays of characters, so something like 'abcd'=='b' returns a [0 1 0 0] logical array... We then use any() to see if any of the values are 1 (true).

Related

generate all possible subset from a character array in MATLAB

I need to generate all possible subset from a character array in MATLAB with reduced execution time.
For example:
input='ABCA';
output ='A',
'B',
'C',
'AB',
'BC',
'CA',
'ABC',
'BCA',
'ABCA'
You can find all these subsets using straight-forward loops. I don't know if it is worth-while vectorizing these, as any vectorization will require large intermediate arrays.
With a random input of 500 characters, and maxLen at 20, I got 4207817 unique substrings. It took my computer (with MATLAB R2017a) 12 seconds to find these. Whether that is fast enough or not is up to you, but I would not bother further optimizing this.
input = 'ABCA';
maxLen = 4;
subsets = {};
for len = 1:maxLen
subs = cell(1,numel(input)-len+1);
for start = 1:numel(subs)
subs{start} = input(start:start+len-1);
end
subs = unique(subs);
subsets = [subsets,subs];
end
disp(subsets)
Output:
'A' 'B' 'C' 'AB' 'BC' 'CA' 'ABC' 'BCA' 'ABCA'
If it is important to preserve the order of the substrings, then add the 'stable' argument to the unique call:
subs = unique(subs,'stable');
For example, for input = 'AFCB';, the output without 'stable' is:
'A' 'B' 'C' 'F' 'AF' 'CB' 'FC' 'AFC' 'FCB' 'AFCB'
and with 'stable' it is:
'A' 'F' 'C' 'B' 'AF' 'FC' 'CB' 'AFC' 'FCB' 'AFCB'

How to create a sub-cell from a cell array in Matlab?

Lets say I have following cell array data in Matlab:
>> data = {'first', 1; 'second', 2; 'third', 3}
data =
'first' [1]
'second' [2]
'third' [3]
Then I want to create a new cell array which has only the first column data. I tried the following but got only the first value instead.
>> column_1 = data{:,1}
column_1 =
first
But what I would like to get as output is:
>> column_1 = {'first';'second';'third'}
column_1 =
'first'
'second'
'third'
How can I create a sub-cell from first column of data cell array?
You have to use round parentheses indexing instead of curly braces indexing, like this:
data(:,1)
Output:
ans =
3×1 cell array
'first'
'second'
'third'
Basically, the purpose of curly braces is to retrieve the underlying content of cells and present a different behavior. For extracting subsets of cells you need to use round parentheses. For more details, refer to this page of the official Matlab documentation.

Create variables from an array of cells in Matlab

I have an array of cells, for example,
cells = {'a', 'b', 'c', d', 'e'};
which is inside a for loop of 1 to 5.
I want to create a variable from a to e depending on the loop index, as 1 to a, 2 to b...
When I try (i is the for index),
eval(cells{i}) = values; it gives me the error,
Undefined function or method 'eval' for input arguments of type 'a'
Here the answer:
eval(sprintf([cells{i} '=values;']))
And you can remove the ; if you want to see the display in command window.
In answer to your comment :
cells = {'a', 'b', 'c', 'd', 'e'};
values = 4;
i = 1;
eval(sprintf([cells{i} '=values;']))
This works perfectly fine on my computer, and i get no warning or error messages.
when calling eval, all arguments must be strings, so convert your cell elements to strings first.
eval([ cellstr(cells{i}) ' = values;']))

Compare two cell array elements in matlab

I am trying to compare two cell arrays, 1x160 (a) and 80x1(b). My cell arrays consist of cells which have a number of strings inside. I wanna compare each string ans see if they are equal, then if they are equal, insert to new array, or insert 0 otherwise. I can't find any function for that. I tried 'isequal','strfind' and others. All of them give me next error message:
If any of the input arguments are cell arrays, the first must be a
cell array of strings and the second must be a character array.
Here is my code!
function [inter]=Intersect2(a,b)
int=cell(0);
b2=[b;b];
for i=1:length(a)
if a{i,1}==b2{i,1}(1) ( or 'isequal','strfind')
int{i}=a{i};
else
int{i}=0;
end
end
There are many ways to compare character arrays, one of which is strcmp.
We'll use cellfun as well to avoid looping.
a = {'Dude', 'I', 'am', 'a', 'moose'};
b = {'Well', 'I', 'am', 'a', 'mouse'};
index = cellfun(#strcmp, a, b);
This will compare each element of a against the corresponding element in b, returning a logical array index that is 1 when the elements match and 0 when they do not.
Use this to assign matching values:
int = cell(1, length(a));
int(index) = a(index);
int =
[] 'I' 'am' 'a' []
You can extend this concept to find the set intersection if you wish.

Why do I get this error inserting a character into a MATLAB matrix?

I am constructing a 16x16 matrix, consisting of just letters in MATLAB. I tried for example:
for i=1:2:3
C(i,2)=char('B');
end
to place the letter 'B' in its corresponding place in the matrix. However this gives a value of 66 in the matrix instead of just the letter 'B'. What is going wrong?
The problem is most likely that you already have a variable called C that contains numeric data in it. When you try to place a character into a numeric matrix, the character gets converted to its ASCII value. If you clear the variable C before running your above code, you should get a character matrix for C:
>> clear C
>> for i=1:2:3, C(i,2) = 'B'; end
>> C
C =
B
B
Note in this case that C is a 3-by-2 array with null characters (ASCII code 0) in the first column and second row of the second column. If you want to initialize C to be a 16-by-16 character array of null characters, you can replace the CLEAR statement in the above code with:
C = char(zeros(16));
And then run your loop to fill in your values. Also note that char('B') is redundant, since 'B' is already of type character.
Matlab stores the letter 'B' as integer ASCII code. Ini fact, char means int8.
If I do
for i=1:3
C(i)=char('A');
end
I get C=AAA, as you'd expect. I suspect the reason why you're getting the decimal value of the char is because you might have preallocated C as C=zeros(16). MATLAB initialized the array as numeric type and accordingly, when you replaced an element with a char, it promptly converted it to its numeric value.
A better approach would be to use cells and then convert it to a matrix.
C=cell(4,4);%# create an empty 4x4 cell
for i=1:16
C{i}=char('A');
end
C=
'A' 'A' 'A' 'A'
'A' 'A' 'A' 'A'
'A' 'A' 'A' 'A'
'A' 'A' 'A' 'A'
Now use cell2mat to convert it to a matrix:
Cmatrix=cell2mat(C)
Cmatrix=
AAAA
AAAA
AAAA
AAAA
Normally, I wouldn't use loops, but I don't know your exact requirements, so I've shown an example as per your question.