订阅分配维度不匹配(matlab)

时间:2015-10-02 16:37:55

标签: matlab matrix vector

代码适用于input_cell= {'ABC','ACB','BCA'}

代码无法针对input_cell= {'ABC','ACB','BCAD'}

运行

有人可以帮我解决这个错误吗?

 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 个答案:

答案 0 :(得分:0)

您可以使用结构来解决此问题。在结构中,每个单元的尺寸可以不同。

答案 1 :(得分:0)

如果您的data变量可以是单元格,则可行:

 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

产生的数据变量:

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

希望这会有所帮助