필터 지우기
필터 지우기

Find the first value of each groups.

조회 수: 9 (최근 30일)
Smithy
Smithy 2023년 5월 24일
댓글: Smithy 2023년 5월 25일
Hello everybody,
I would like to find the first value of each groups. Is there a way to find the each group's first value?
the expecting answer is [1 5 2 1] in this case.
I tried with
GroupFirst1 = arrayfun(@(k) result(find(result(ic==k), 1, 'first')),1:length(GroupId))';
but it doest not work. Could anyone give me some helps?
x = [3 3 3 4 4 5 5 5 5 3 11]';
y = [1 2 4 5 7 2 1 8 10 10 1]';
result = [x,y];
[GroupId,~,ic] = unique(result(:,1));
% Calculating the averages of different groups of values
GroupMean1 = arrayfun(@(k) mean(result(ic==k,2)),1:length(GroupId))';
% How to find the first y value of according to x
% I need to get 1st value of Ys, corresponds to similar groups of Xs separately.
% first y value when x = 3 is.. 1, and first y value when x = 4 is.. 5, and
% first y value when x = 5 is.. 2, and first y value when x = 11 is.. 1.
% so the expecting answer is [1 5 2 1]
% GroupFirst1 = arrayfun(@(k) result(find(result(ic==k), 1, 'first')),1:length(GroupId))'; % it doest not work.

채택된 답변

Raghava S N
Raghava S N 2023년 5월 24일
편집: Raghava S N 2023년 5월 24일
x = [3 3 3 4 4 5 5 5 5 3 11]';
y = [1 2 4 5 7 2 1 8 10 10 1]';
result = [x,y];
[GroupId,~,ic] = unique(result(:,1));
GroupFirst1 = arrayfun(@(k) result(find(result(:,1)==GroupId(k),1),2),1:length(GroupId))'
GroupFirst1 = 4×1
1 5 2 1
This should work. I think you made the mistake of not searching for the 'GroupId's, but their indices with result(ic==k).
  댓글 수: 1
Smithy
Smithy 2023년 5월 25일
Thank you very much for your answer. I really really really appreciate with it. It works really well now.

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

추가 답변 (2개)

Luca Ferro
Luca Ferro 2023년 5월 24일
It's not the most efficient way since it uses loops and find instead of indexing but this works:
x = [3 3 3 4 4 5 5 5 5 3 11]';
y = [1 2 4 5 7 2 1 8 10 10 1]';
result = [x,y];
[GroupId,~,ic] = unique(result(:,1));
for gg=1:size(GroupId,1)
allCorr=find(x==GroupId(gg));
firstCorr(gg)=y(allCorr(1));
end
firstCorr
firstCorr = 1×4
1 5 2 1
  댓글 수: 1
Smithy
Smithy 2023년 5월 25일
Thank you very much for your huge helps. It works for me reall well.

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


Edoardo Mattia Piccio
Edoardo Mattia Piccio 2023년 5월 24일
Hi Smithy, here there is an attempt to solve your problem. I hope it will be all clear, this is my first answer here
x = [3 3 3 4 4 5 5 5 5 3 11]';
y = [1 2 4 5 7 2 1 8 10 10 1]';
[newX,idx]= sort(x,'ascend'); % Sort x so the equal numbers are consecutive
newY= y(idx); % sort y in the same way of x
idx2= [1; diff(newX)~=0];% with diff(newX) I find when the number changes; adding 1 as first element, to taking account the first group of equal numbers
temp= newY.*idx2; % take only the numbers of y that corresponds to the first value of x
GroupFirst1= temp(temp>0);
  댓글 수: 1
Smithy
Smithy 2023년 5월 25일
Really thank you very much for your helps. It works for me reall well.

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

카테고리

Help CenterFile Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

태그

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by