Mapping arrays based on repeating integers in a vector

2 views (last 30 days)
%%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

Accepted Answer

Jan
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.
  1 Comment
Stephen Devlin
Stephen Devlin on 20 Jul 2017
Edited: Stephen Devlin on 20 Jul 2017
Hi Jan, The data in the first column signifies which photo/frame the coordinates of my objects are, they are in a random order and once i have them split into the separate arrays to can index the correct frame simply I need to process the data further to obtain trajectories and velocities for the objects. For that I need to determine which drop is the strobed delay of its previous drop and so on. You may well look at the code above and see that it is all there already but I'm missing that insight (still new at this).
I played around with [C,ia,ic]=Unique(A) function last night and it gives me a few useful bits but I still need to work out how to programmatically work on one single frame at a time before processing the next and so on.
Maybe 'ugly' isn't the right word but I keep reading that vector indexing is the preferred approach to matlab rather than a for loop. The preallocation I was going to add but wasn't sure whether logical or relative indexing would work best, relative seemed easier to see in my head but couldn't see how I would use logicals.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!