Find groups of duplicate values and preserving order?

조회 수: 7 (최근 30일)
Trung Ngo
Trung Ngo 2019년 6월 28일
댓글: Trung Ngo 2019년 7월 2일
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
Rik 2019년 7월 2일
Can you make a small example to explain what you want?
Trung Ngo
Trung Ngo 2019년 7월 2일
Hi Rik, here is my code
[B, I] = sort(check_value);
lat_sorted=lat(I,:);
lon_sorted=lon(I,:);
for i=1:length(B)
for j=1:length(B)-1
if B(i+j)==B(i)
if B(i)==11
L = load('110.mat');
elseif B(i)==12
L = load('120.mat');
elseif B(i)==13
L = load('130.mat');
elseif B(i)==14
L = load('140.mat');
elseif B(i)==15
L = load('150.mat');
elseif B(i)==16
L = load('160.mat');
elseif B(i)==17
L = load('170.mat');
elseif B(i)==18
L = load('180.mat');
elseif B(i)==19
L = load('190.mat');
elseif B(i)==191
L = load('191.mat');
elseif B(i)==192
L = load('192.mat');
elseif B(i)==193
L = load('193.mat');
elseif B(i)==21
L = load('210.mat');
elseif B(i)==22
L = load('220.mat');
elseif B(i)==23
L = load('230.mat');
elseif B(i)==24
L = load('240.mat');
elseif B(i)==25
L = load('250.mat');
elseif B(i)==26
L = load('260.mat');
elseif B(i)==27
L = load('270.mat');
elseif B(i)==28
L = load('280.mat');
elseif B(i)==29
L = load('290.mat');
elseif B(i)==291
L = load('291.mat');
elseif B(i)==292
L = load('292.mat');
elseif B(i)==293
L = load('293.mat');
elseif B(i)==31
L = load('310.mat');
elseif B(i)==32
L = load('320.mat');
elseif B(i)==33
L = load('330.mat');
elseif B(i)==34
L = load('340.mat');
elseif B(i)==35
L = load('350.mat');
elseif B(i)==36
L = load('360.mat');
elseif B(i)==37
L = load('370.mat');
elseif B(i)==38
L = load('380.mat');
elseif B(i)==39
L = load('390.mat');
elseif B(i)==391
L = load('391.mat');
elseif B(i)==392
L = load('392.mat');
elseif B(i)==393
L = load('393.mat');
end
for l=i:i+j
tf(l) = interp2(L.x,L.y,L.is_land,lon_sorted(l),lat_sorted(l),'cubic');
end
elseif B(i+j)~=B(i)
continue;
end
end
end
As you can see B(1)=B(2)=B(3)=23 and I do not want to load them 3 times, I just want to load them 1 time only and run tf(l) 3 times for l=i:i+j (in this case i=1 and j is from 1 to 2)

댓글을 달려면 로그인하십시오.

채택된 답변

Rik
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
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)
Trung Ngo
Trung Ngo 2019년 7월 2일
Thanks Rik for your help, wish you the best

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by