Matlab 查找以下值并在矩阵中表示它们的出现
我有一个矩阵Matlab 查找以下值并在矩阵中表示它们的出现,matlab,matrix,Matlab,Matrix,我有一个矩阵a: A = [23 34 45 0 0 0 ; 21 34 0 0 23 11 ; 34 23 0 0 0 22 ; 23 11 21 0 0 45 ; 11 45 23 0 0 0 ] 我在矩阵中找到了唯一的值: U = unique(A) = [0; 11; 21; 22; 23; 34; 45] 除了值0,我想要一个矩阵6x6(6是不带零
a
:
A = [23 34 45 0 0 0 ;
21 34 0 0 23 11 ;
34 23 0 0 0 22 ;
23 11 21 0 0 45 ;
11 45 23 0 0 0 ]
我在矩阵中找到了唯一的值:
U = unique(A) = [0; 11; 21; 22; 23; 34; 45]
除了值0
,我想要一个矩阵6x6(6是不带零的值的数目),其中我想要表示一个值从每行中的另一个值跟随的次数
例如:
发生在11
之后的11
是0
。
发生在
11
之后的21
是1
。
发生在
11
之后的22
是0
。
发生在
11
之后的23
是0
。
在
11
之后出现34
是0
。
发生在
11
之后的45
是1
因此,我想要的矩阵的第一行是:B=[011]
发生在21
之后的11
是0
。
在
21
之后出现21
是0
。
在
21
之后出现22
是0
。
发生在
21
之后的23
是0
。
发生在
21
之后的34
是1
。
在
21
之后出现45
是1
所以矩阵的第二行是
B = [0 1 0 0 0 1; 0 0 0 0 1 1; ...]
我想对U
中的所有值重复相同的过程
你能帮我吗?可能是因为我在使用SAN编译器,所以这里的某个地方输入错误,但一般策略是:创建掩码、应用掩码、计数
U = U(2:end); %Remove 0 value from unique matrix
output = zeros(length(U));
for ii = 1:length(U)
maskA = cumsum(A == U(ii),2); #Find any values of A = U(ii), and make them + all to the right equal to 1
maskA = [zeros(size(A,1),1) maskA(:,1:size(A,2)-1)] %Shift highlighting to the right one column
maskedA = A*maskA; %Filter out only values we want to count
for kk = 2:length(U)
output(ii,jj) = sum(maskedA(:)==U(ii)); %Count number of matching values
end
end
由于我使用的是sans编译器,所以这里的某个地方可能有一个输入错误,但总体策略是:创建掩码、应用掩码、计数
U = U(2:end); %Remove 0 value from unique matrix
output = zeros(length(U));
for ii = 1:length(U)
maskA = cumsum(A == U(ii),2); #Find any values of A = U(ii), and make them + all to the right equal to 1
maskA = [zeros(size(A,1),1) maskA(:,1:size(A,2)-1)] %Shift highlighting to the right one column
maskedA = A*maskA; %Filter out only values we want to count
for kk = 2:length(U)
output(ii,jj) = sum(maskedA(:)==U(ii)); %Count number of matching values
end
end
这是一个可能的解决方案:
A = [23 34 45 0 0 0 ;
21 34 0 0 23 11 ;
34 23 0 0 0 22 ;
23 11 21 0 0 45 ;
11 45 23 0 0 0 ]
% final result is a 6 * 6 table we want to map from 11; 21; 22; 23; 34; 45 to 1:6
% first sort the array
[S SI] = sort(A(:));
% then generate mapped values corresponding to original values
S2=[0; (cumsum(diff(S)>0))];
% then replace original with mapped value
A(SI) = S2;
% use circshift to create a matrix that brings next element in each row to one left ,
% so current value in original matrix and next value in cricshifted matrix are in the same position.
C=circshift(A,[0,-1]);
% so both matrices converted to vectors and horizontally concatenated to a n * 2 matrix (U) ,
% its first column is the current element in each row and second column is the following element.
% since for example there may be multiple cases of [11 21] ,
% we take unique of the U matrix to remove repeated co-occurances.
U = unique( [A(1:end-size(A,1)); C(1:end-size(A,1))]','rows');
% zero values should be discarded then we get indices of rows that contain zero
[ro ,~] = find(U == 0);
uro = unique(ro);
% rows that contain zero excluded from two column matrix (U).
U(uro,:) =[];
% now first column of U contains indices of rows of 1s and second column indices of their columns.
% then convert indices to 0-1 matrix
result = full(sparse(U(:,1),U(:,2),1))
这是一个可能的解决方案:
A = [23 34 45 0 0 0 ;
21 34 0 0 23 11 ;
34 23 0 0 0 22 ;
23 11 21 0 0 45 ;
11 45 23 0 0 0 ]
% final result is a 6 * 6 table we want to map from 11; 21; 22; 23; 34; 45 to 1:6
% first sort the array
[S SI] = sort(A(:));
% then generate mapped values corresponding to original values
S2=[0; (cumsum(diff(S)>0))];
% then replace original with mapped value
A(SI) = S2;
% use circshift to create a matrix that brings next element in each row to one left ,
% so current value in original matrix and next value in cricshifted matrix are in the same position.
C=circshift(A,[0,-1]);
% so both matrices converted to vectors and horizontally concatenated to a n * 2 matrix (U) ,
% its first column is the current element in each row and second column is the following element.
% since for example there may be multiple cases of [11 21] ,
% we take unique of the U matrix to remove repeated co-occurances.
U = unique( [A(1:end-size(A,1)); C(1:end-size(A,1))]','rows');
% zero values should be discarded then we get indices of rows that contain zero
[ro ,~] = find(U == 0);
uro = unique(ro);
% rows that contain zero excluded from two column matrix (U).
U(uro,:) =[];
% now first column of U contains indices of rows of 1s and second column indices of their columns.
% then convert indices to 0-1 matrix
result = full(sparse(U(:,1),U(:,2),1))
如果你用1:6来代替11:21,那就容易多了。我不知道怎么做。。。我想在矩阵A中找到所有可能的值的组合,但这不是一个聪明的想法。你想使用一个
circshift(A,[0,-1])
得到包含所有元素的矩阵,你想去掉A的最后一行和这个矩阵。把这个和A结合起来,你应该得到5*5或25组数字,就像我前面说的那样,你可以把它们转换成坐标(如果你把它们替换成1:6,你看?)@elis56你写道:“在21
之后出现45
是1
”这是打字错误吗?它不应该是零吗?如果你用1:6来代替11:21,那就容易多了。我不知道怎么做。。。我想在矩阵A中找到所有可能的值的组合,但这不是一个聪明的想法。你想使用一个circshift(A,[0,-1])
得到包含所有元素的矩阵,你想去掉A的最后一行和这个矩阵。把这个和A结合起来,你应该得到5*5或25组数字,就像我前面说的那样,你可以把它们转换成坐标(如果你把它们替换成1:6,你看?)@elis56你写道:“在21
之后出现45
是1
”这是打字错误吗?它不应该是零吗?还要添加一些解释以使其更易于理解。我认为这是一个很好的答案,而且应该是超快速的(所有并行函数)。@Sardar_Usama解释provided@GameOfThrows干杯也许有更好的解决办法。我在问题下看到了你的评论,但在我完成回答之后。你关于cirshift的想法很有趣,还添加了一些解释,使其更易于理解。我认为这是一个很好的答案,而且应该是超快速的(所有并行函数)。@Sardar_Usama解释provided@GameOfThrows干杯也许有更好的解决办法。我在问题下看到了你的评论,但在我完成回答之后。你关于cirshift的想法很有趣