How to get all possible combinations of array elements without duplicate - matlab

I have a vector of string elements vector = {'A','B','C'}.
I want to generate all possible combination of the three elements of the array but without duplicate.
I expect the following result: {'A', 'B', 'C', 'AB', 'AC', 'BC', 'ABC'}.
How can I do that in MATLAB?

Judging from your desired result, you want all the combinations with 2 choices of 'A', 'B', 'C', and '' (nothing). You can do it with nchoosek as follows
result = nchoosek(' ABC', 2) % note space for empty
Output
result =
6×2 char array
' A'
' B'
' C'
'AB'
'AC'
'BC'
Then removing the spaces and converting the combinations to a cell array:
result = strrep(cellstr(result), ' ', '')
As Wolfie pointed out, this only works for single character input, for multi character inputs we can use string arrays instead of char arrays:
result = nchoosek(["","A1","B2","C3"], 2);
result = result(:,1) + result(:,2) % string cat
% result = cellstr(result); % optional if want cell output
result =
6×1 string array
"A1"
"B2"
"C3"
"A1B2"
"A1C3"
"B2C3"

Related

How to convert 1d array of chars from CSV to 2d cell array in Matlab

I have a function I cannot change in Matlab that converts CSV file to a 1d array of chars.
This is an example of what it looks like:
array_o_chars = ['1' 'd' ',' ' ' 'a' 'r' 'r' 'a' 'y' '\n' 'o' 'f' ',' ' ' 'c' 'h' 'a' 'r' 's'];
I need to convert it to a 2d "Cell" array for the rest of my code, how do I do it?
The answer is surprisingly simple:
First, you want to break the char array into the "rows" of the CSV file. Depending on your line ending, you will choose one of these as a delimiter: \n, \r, or a combination of the two. For this example, we are using \n.
rows = split(array_o_chars, '\n');
Now that you have your rows, you need to create your columns, this is done in the same manner as your rows, except using , as your delimiter, or in this case , (a comma followed by a space).
cell_array = split(rows, ', ');
Now you have the 2d cell array you desire.
All together now:
% Test 1d array
array_o_chars = ['1' 'd' ',' ' ' 'a' 'r' 'r' 'a' 'y' '\n' 'o' 'f' ',' ' ' 'c' 'h' 'a' 'r' 's'];
% Conversion to cell array
rows = split(array_o_chars, '\n');
cell_array = split(rows, ', ');
% Show result in Command Window
display(cell_array);
Output from Matlab:
cell_array =
2×2 cell array
{'1d'} {'array'}
{'of'} {'chars'}

MATLAB string cell array without loop

I am using a loop to create my cell array. It contains the string 'A1' to 'A10'.
Is there a way to iterate without using a loop ?
a = cell( 10, 1 );
for i = 1 : length( a )
a{i} = [ 'A', num2str( i ) ];
end
a =
'A1'
'A2'
'A3'
'A4'
'A5'
'A6'
'A7'
'A8'
'A9'
'A10'
I assume you want to build a without a loop. Let N = 10 as per your example.
Approach 1
a = sprintf('A%i ', 1:N);
a = a(1:end-1);
a = strsplit(a).';
This builds a char vector with a space after each number, removes the final space, splits on spaces, and transposes.
Approach 2
Another approach:
a = deblank(cellstr(strcat('A', strjust(num2str((1:10).'), 'left'))));
This concatenates 'A' with the numbers to form a 2D char array with some spaces; moves the spaces in each row to the right; converts each row into a cell; and removes trailing spaces on each cell.
If you have R2017a or later consider using string arrays instead of cell array of char vectors. You can create your string array using
"A"+(1:10)'

How to convert an string array into a character array in Matlab?

Suppose that we have an string array in Matlab like bellow:
a='This is a book'
How can we convert the above string array into a character array by a function in Matlab like bellow?
b={'T' 'h' 'i' 's' ' ' 'i' 's' ' ' 'a' ' ' 'b' 'o' 'o' 'k'}
Your a is not a string array; it's a character array (which also used to be called a string, but starting from R2016b that term has a different meaning). Your b is not a character array, it's a cell array that contains characters.
Anyway, to convert from a to b, use num2cell:
a = 'This is a book';
b = num2cell(a);
If you really really want to convert string (introduced since R2016b) to char array, this is how you do.
s = "My String"; % Create a string with ""
c = char(s); % This is how you convert string to char.
isstring(c)
ans =
logical
0
ischar(c)
ans =
logical
1

Replacing letters with numbers in a MATLAB array

I am trying to write a function to mark the results of a test. The answers given by participants are stored in a nx1 cell array. However, theses are stored as letters. I am looking for a way to convert (a-d) these into numbers (1-4) ie. a=1, b=2 so these can be compared the answers using logical operations.
What I have so far is:
[num,txt,raw]=xlsread('FolkPhysicsMERGE.xlsx', 'X3:X142');
FolkPhysParAns=txt;
I seem to be able to find how to convert from numbers into letters but not the other way around. I feel like there should be a relatively easy way to do this, any ideas?
If you have a cell array of letters:
>> data = {'a','b','c','A'};
you only need to:
Convert to lower-case with lower, to treat both cases equally;
Convert to a character array with cell2mat;
Subtract (the ASCII code of) 'a' and add 1.
Code:
>> result = cell2mat(lower(data))-'a'+1
result =
1 2 3 1
More generally, if the possible answers are not consecutive letters, or even not single letters, use ismember:
>> possibleValues = {'s', 'm', 'l', 'xl', 'xxl'};
>> data = {'s', 'm', 'xl', 'l', 'm', 'l', 'aaa'};
>> [~, result] = ismember(data, possibleValues)
result =
1 2 4 3 2 3 0
Thought I might as well write an answer...
you can use strrep to replace 'a' with '1' (note it is the string format), and do it for all 26 letters and then use cell2mat to convert string '1' - '26' etc to numeric 1 -26.
Lets say:
t = {'a','b','c'} //%Array of Strings
t = strrep(t,'a','1') //%replace all 'a' with '1'
t = strrep(t,'b','2') //%replace all 'b' with '2'
t = strrep(t,'c','3') //%replace all 'c' with '3'
%// Or 1 line:
t = strrep(g,{'a','b','c'},{'1','2','3'})
>> t =
'1' '2' '3'
output = cellfun(#str2num,t,'un',0) //% keeps the cell structure
>> output =
[1] [2] [3]
alternatively:
output = str2num(cell2mat(t')) //% uses the matrix structure instead, NOTE the inversion ', it is crucial here.
>> output =
1
2
3

Updating N-gram 2 dimension cell array in Matlab

I am trying to extract bi-grams from a set of words and store them in a matrix. what I want is to insert the word in the first raw and all the bi-grams related to that word
for example: if I have the following string 'database file there' my output should be:
database file there
da fi th
at il he
ta le er
ab re
..
I have tried this but it gives me only the bigram without the original word
collection = fileread('e:\m.txt');
collection = regexprep(collection,'<.*?>','');
collection = lower(collection);
collection = regexprep(collection,'\W',' ');
collection = strtrim(regexprep(collection,'\s*',' '));
temp = regexprep(collection,' ',''',''');
eval(['words = {''',temp,'''};']);
word = char(words(1));
word2 = regexp(word, sprintf('\\w{1,%d}', 1), 'match');
bi = cellfun(#(x,y) [x '' y], word2(1:end-1)', word2(2:end)','un',0);
this is only for the first word however, i want to do that for every word in the "words" matrix 1X1000
is there an efficient way to accomplish this as I will deal with around 1 million words?
I am new to Matlab and if there any resource to explain how to deal with matrix (update elements, delete, ...) will be helpful
regards,
Ashraf
If you were looking to get a cell array as the output, this might work for you -
input_str = 'database file there' %// input
str1_split = regexp(input_str,'\s','Split'); %// split words into cells
NW = numel(str1_split); %// number of words
char_arr1 = char(str1_split'); %//' convert split cells into a char array
ind1 = bsxfun(#plus,[1:NW*2]',[0:size(char_arr1,2)-2]*NW); %//' get indices
%// to be used for indexing into char array
t1 = reshape(char_arr1(ind1),NW,2,[]);
t2 = reshape(permute(t1,[2 1 3]),2,[])'; %//' char array with rows for each pair
out = reshape(mat2cell(t2,ones(1,size(t2,1)),2),NW,[])'; %//'
out(reshape(any(t2==' ',2),NW,[])')={''}; %//' Use only paired-elements cells
out = [str1_split ; out] %// output
Code Output -
input_str =
database file there
out =
'database' 'file' 'there'
'da' 'fi' 'th'
'at' 'il' 'he'
'ta' 'le' 'er'
'ab' '' 're'
'ba' '' ''
'as' '' ''
'se' '' ''