我很难找到一个 matlab 函数/代码来执行以下任务
我有一个向量 C = [1 1 2 2 2 3 3 4]
我需要结果向量 Y = [1 2 1 2 3 1 2 1]
You could create a function like the following:
C = [1 1 2 2 2 3 3 4]
Y = zeros(1,length(C))
helper = zeros(1,max(C)) % stores the count for each value
for i=1:length(C)
helper(C(i)) = helper(C(i))+1; %increases the count for the value in C(i)
Y(i) = helper(C(i));
end
Hope that helps
试试这个,如果你想要它在一个衬里,这将工作......
Y = sum(cumsum(meshgrid(C)==meshgrid(C)',2).*(meshgrid(C)==meshgrid(C)').*eye(length(A)),1);
不是最漂亮的,但它会起作用(您可以随时将其拆分以使其更清晰)