Mapping arrays based on repeating integers in a vector
조회 수: 2 (최근 30일)
이전 댓글 표시
%%make some fake data
A=(1:5)'
B=zeros(10,1)
B(1:2:end)=A(1:1:end)
B(2:2:end)=A(1:1:end)
B(3)=1
B(9)=4
D=(rand(10,2))*100;
E=horzcat(B,D)
E(11,:)=[5,40,66.1]
%%******************************************************************
maxnum=max(E(:,1))
%%******************************************************************
% Need to map the values from columns 2&3 for the rows where they have the
% same value in the first colum. I could write a loop that will split them up
% into the 1's, 2's and 3's but I am wondering if there is a tidier vector
% approach using logical or relative indexing
%%******************************************************************
Z=unique(E(:,1));
%%******************************************************************
for k=1:length(Z)
S{k}= find(E(:,1)==k);
end
Hi,
I have been looking through questions on here and stack overflow and wondered if anyone has any pointers? I have a large dataset of coordinates of objects taken from multiple frames. I have an index as to which frame the coordinates are taken. I would like to either split up the array or be able to index that portion of the array which corresponds to the frame I am interested in to analyse. I have tried a few methods involving loops, none of which worked particularly well, the example above has the frame info in the cell array S but its an ugly way to do it and want to know if there is a better way.
Best regards
Steve
댓글 수: 0
채택된 답변
Jan
2017년 7월 19일
What do you find ugly here?
S = cell(1, length(Z));
for k=1:length(Z)
S{k} = (E(:,1) == k);
end
With a pre-allocation and using logical indexing, this would be efficient and clean. But of course te required information is contained completely in the first column already, so why do you need to store this indexing correlation separately? Using "E(:,1) == k" later in the code would be sufficient also.
추가 답변 (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!