0
votes

The code is ok for input_cell= {'ABC','ACB','BCA'}

The code can not run for input_cell= {'ABC','ACB','BCAD'}

Can anyone help me to fix this error please?

 input_cell= {'ABC','ACB','BCAD'}
 %the code is ok for  input_cell= {'ABC','ACB','BCA'}
 data=[];
for i=1:numel(input_cell)

p=strsplit(input_cell{i},' ')
m=cell2mat(p(:))
m=m-'?'

[~,k]=sort(m);
%compare each pair
M = bsxfun(@(a,b)(a<b)+0.5*(a==b),k,k')
vector = reshape(M.',[],1)  %# Collect the row contents into a column vector
vector = vector' % change from columns into 1 row
data(i,:)= vector  % data matrix inludes all vectors
end
2
One of your input strings is 4 characters while the others are 3. I haven't written an answer because I don't know what this code is doing to correct and give you your desired output. - rayryeng
what does strsplit do - Abr001am
@kgk did you get it to work? - jack

2 Answers

0
votes

You can fix this by using a struct. In structs the dimensions of each cell can differ.

0
votes

If your data variable can be a cell, this works:

 input_cell= {'ABC','ACB','BCAD'}
 %the code is ok for  input_cell= {'ABC','ACB','BCA'}
 data={};
for i=1:numel(input_cell);
p=input_cell{i}(:);
[~,k]=sort(p);
%compare each pair
M = bsxfun(@(a,b)(a<b)+0.5*(a==b),k,k')
vector = reshape(M.',[],1)  %# Collect the row contents into a column vector
vector = vector' % change from columns into 1 row
data{i}= num2cell(vector(:,:))  % data matrix inludes all vectors
end

Resulting data variable:

data = {1x9 cell}    {1x9 cell}    {1x16 cell}

hope this helps