Find groups of duplicate values and preserving order?
4 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Rik
2019 年 7 月 2 日
編集済み: Rik
2019 年 7 月 2 日
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 件のコメント
Rik
2019 年 7 月 2 日
Well spotted. I've edited my answer accordingly. (I've shortened your comment to include only the change for readability of this thread)
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!