Mapping arrays based on repeating integers in a vector
2 views (last 30 days)
Show older comments
%%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 Comments
Accepted Answer
Jan
on 19 Jul 2017
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.
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!