필터 지우기
필터 지우기

Sync cell arrays using unique

조회 수: 1 (최근 30일)
Joel Schelander
Joel Schelander 2021년 4월 20일
답변: Chetan 2024년 2월 28일
I have two cells GUD and GUDID, GUD is containing a 1x1000 double and GUDID contains a cell for every element in GUD.
I am calculating on GUD here. GUDID contains one ID number for every element in GUD
GUD={[2.56 3.23 1.22....]}
GUDID={173 178 180....}
for lol = 1:numel(GUD)
GUD{lol}=unique(GUD{1,lol});
%GUDID{lol}=...?
end
  댓글 수: 1
Rik
Rik 2021년 4월 20일
It is not clear to me what you question is or what you want to do.
One tiny speedup: as long as you avoid some classes (strings, tables, timetables, and more), you can use the legacy syntax:
idx = ~cellfun('isempty',GUD{lol});

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

답변 (1개)

Chetan
Chetan 2024년 2월 28일
I understand you need to synchronize your `GUDID` cell array with the unique values obtained from the `GUD` cell array.
To achieve this, you can utilize the index output from the `unique` function to subset the corresponding IDs.
Here's the modified loop with some mock data:
% Mock data
GUD = {[2.56, 3.23, 1.22, 2.56, 3.23], [1.11, 1.22, 1.11]};
GUDID = {[173, 178, 180, 173, 178], [190, 180, 190]};
% Process to get unique values and corresponding IDs
for lol = 1:numel(GUD)
[GUD{lol}, ia] = unique(GUD{1,lol}, 'stable'); % 'stable' keeps original order
GUDID{lol} = GUDID{lol}(ia); % Subset the IDs based on unique value indices
end
GUD
GUD = 1×2 cell array
{[2.5600 3.2300 1.2200]} {[1.1100 1.2200]}
GUDID
GUDID = 1×2 cell array
{[173 178 180]} {[190 180]}
By using `ia`, you ensure `GUDID` reflects the changes in `GUD`.
For more information on the `unique` function and indexing refer to following MathWorks Documentation:
Thanks
Chetan

카테고리

Help CenterFile Exchange에서 Programming Utilities에 대해 자세히 알아보기

태그

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by