从给定长度不同的字符串单元格数组创建矩阵 [英] Create matrices from a given cell-array of strings with different lengths
问题描述
我在一个细胞阵列中有3个序列:
I have 3 sequences in a cell-array:
Input_cell= {'ABCD','ACD', 'ABD'}
S1= 'ABCD' % which means A<B<C<D
S2= 'ACD' % which means A<C<D % missing B in the full string of 'ABCD'
S3= 'ABD' % which means A<B<D % missing C in the full string of 'ABCD'
我想将Input_cell
中的每个字符串转换为必须满足以下条件的矩阵M
(i
-by- j
):
I want to convert each of the strings in the Input_cell
into a matrix M
(i
-by-j
) which has to satisfy these conditions:
-
M(i,j)
和M(j,i)
是随机的 -
M(i,i) = 0.5
-
M(i,j) + M(j,i) = 1
-
M(i,j) < M(j,i)
例如,如果A<B
然后M(A,B) < M(B,A)
M(i,j)
andM(j,i)
are randomM(i,i) = 0.5
M(i,j) + M(j,i) = 1
M(i,j) < M(j,i)
For example ifA<B
thenM(A,B) < M(B,A)
例如,如果我们有S1 = 'ABCD'
(表示A<B<C<D
),则将按以下方式预期M1
矩阵:
For example if we have S1 = 'ABCD'
(which means A<B<C<D
), the M1
matrix will be expected as follows:
A B C D
A 0.5 0.3 0.2 0.1
B 0.7 0.5 0 0.4
C 0.8 1 0.5 0.1
D 0.9 0.6 0.9 0.5
如果我们有S2 = 'ACD'
(表示A<C<D
),而在'ABCD'
的完整字符串中缺少B
,我们会将值0.5
放在矩阵中B
的每个位置, M2
矩阵应如下所示:
If we have S2 = 'ACD'
(which means A<C<D
), missing B
in the full string of 'ABCD'
, we will put the value 0.5
in every position of B
in the matrix, the M2
matrix will be expected as follows:
A B C D
A 0.5 0.5 0.2 0.1
B 0.5 0.5 0.5 0.5
C 0.8 0.5 0.5 0.1
D 0.9 0.5 0.9 0.5
如果我们有S3 = 'ABD'
(表示A<B<D
),而在'ABCD'
的完整字符串中缺少C
,我们会将值0.5
放在矩阵中C
的每个位置, M3
矩阵预计如下:
If we have S3 = 'ABD'
(which means A<B<D
), missing C
in the full string of 'ABCD'
, we will put the value 0.5
in every position of C
in the matrix, the M3
matrix will be expected as follows:
A B C D
A 0.5 0.4 0.5 0.1
B 0.6 0.5 0.5 0.3
C 0.5 0.5 0.5 0.5
D 0.9 0.7 0.5 0.5
如何从给定的序列单元格数组中创建上述矩阵?
How to create that kind of above matrices from a given cell-array of sequences?
推荐答案
首先,您需要找出如何仅针对单个序列执行此操作:
Firstly you need to work out how to do this just for a single sequence:
-
创建一个在
0.5
和1
之间的随机数矩阵:
Create a matrix of random numbers between
0.5
and1
:
M = 0.5*rand(4) + 0.5;
将主对角线设置为等于0.5
M(logical(eye(4))) = 0.5;
设置M
的上三角等于1-下三角:
Set the upper triangle of M
equal to 1 - the lower triangle:
M(triu(true(4))) = 1 - M(tril(true(4))); %// Note the main diagonal doesn't matter...
找出缺少的字母,并相应地将行和列设置为等于0.5
:
fullSeq = 'abcd';
idx = find(fullSeq == setdiff(fullSeq, 'abd'));
%// at this point you'll need to check if idx is empty first...
M(:,idx) = 0.5;
M(idx,:) = 0.5;
现在您可以针对一个矩阵执行此操作,只需遍历单元格数组或将其封装到函数中并使用cellfun
.
And now that you can do it for one matrix, just loop over your cell array or else encapsulate this into a function and use cellfun
.
这篇关于从给定长度不同的字符串单元格数组创建矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!