Find groups of duplicate values and preserving order?
7 views (last 30 days)
Show older comments
Hi all,
I have a 100x1 double matrix
matrix=[100--100].*rand(100,1)-100;
C=unique(matrix);
I would love to create a length(C) x length(matrix) matrix which contains the positioning index from matrix to later conduct a function.
Thank you for your time,
4 Comments
Accepted Answer
Rik
on 2 Jul 2019
Edited: Rik
on 2 Jul 2019
The code below is far from optimal, but it should help you out. It assumes there is always an L to load, and you have enough memory to store all the different mat files in a library. These assumptions are to memoization. You could improve the readability of this code by creating a masterlist of indices and file names, instead of endless elseifs. Then it is very easy to add or remove a new mat and it makes your code more readable. You can also check if the output of ismember sums to 1 to check for valid inputs.
function L=load_mat(Bi)
persistent L_libary
if isempty(L_libary),L_libary={};end
if numel(L_libary)>=Bi && ~isempty(L_libary{Bi})
L=L_libary{Bi};
end
if Bi==11
L = load('110.mat');
elseif Bi==12
L = load('120.mat');
elseif Bi==13
L = load('130.mat');
elseif Bi==14
L = load('140.mat');
elseif Bi==15
L = load('150.mat');
elseif Bi==16
L = load('160.mat');
elseif Bi==17
L = load('170.mat');
elseif Bi==18
L = load('180.mat');
elseif Bi==19
L = load('190.mat');
elseif Bi==191
L = load('191.mat');
elseif Bi==192
L = load('192.mat');
elseif Bi==193
L = load('193.mat');
elseif Bi==21
L = load('210.mat');
elseif Bi==22
L = load('220.mat');
elseif Bi==23
L = load('230.mat');
elseif Bi==24
L = load('240.mat');
elseif Bi==25
L = load('250.mat');
elseif Bi==26
L = load('260.mat');
elseif Bi==27
L = load('270.mat');
elseif Bi==28
L = load('280.mat');
elseif Bi==29
L = load('290.mat');
elseif Bi==291
L = load('291.mat');
elseif Bi==292
L = load('292.mat');
elseif Bi==293
L = load('293.mat');
elseif Bi==31
L = load('310.mat');
elseif Bi==32
L = load('320.mat');
elseif Bi==33
L = load('330.mat');
elseif Bi==34
L = load('340.mat');
elseif Bi==35
L = load('350.mat');
elseif Bi==36
L = load('360.mat');
elseif Bi==37
L = load('370.mat');
elseif Bi==38
L = load('380.mat');
elseif Bi==39
L = load('390.mat');
elseif Bi==391
L = load('391.mat');
elseif Bi==392
L = load('392.mat');
elseif Bi==393
L = load('393.mat');
end
L_libary{Bi}=L;
end
6 Comments
Rik
on 2 Jul 2019
Well spotted. I've edited my answer accordingly. (I've shortened your comment to include only the change for readability of this thread)
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!