如何使列向量的每个索引只重复两次

时间:2016-12-18 08:56:22

标签: matlab matrix

我有一个矩阵(10,20,4)和一个列向量(10)。我创建了一个矩阵,每行只从列向量中选择一个索引。但是,如何使列向量的每个索引只重复两次。

   userDistance=randint(10,20,4);
    K_max=20;
    NN=4;

    for nn=1:NN
        for ue=1:K_max
            temp = userDistances(:,ue,nn); 
            [~,sort_temp] = sort(temp,1,'ascend');
            UE_selected(ue,nn)=sort_temp(1);
        end  
    end

1 个答案:

答案 0 :(得分:0)

我不清楚如果你要求这个,但是有一个索引为v的列向量,下面的代码会过滤出两次以上的值,保留原始顺序:

v = randi(5, 1, 15)'
u = unique(v); % different values
A = repmat(v, 1, length(u)) == repmat(u', length(v), 1);
B = cumsum(A) .* A; % accumulate ocurrences per row
m = max(B');
output = v(m <= 2) % only those that appeared up to 2 times.

执行示例:

v =

 4
 1
 2
 1
 1
 5
 4
 2
 5
 1
 3
 2
 4
 4
 1

输出=

 4
 1
 2
 1
 5
 4
 2
 5
 3
相关问题