Structure from shaperead: Logical indexing gives a 'one row shift'
3 次查看(过去 30 天)
显示 更早的评论
Dear all, I am facing a issue regarding logical indexing of elements in a structure loaded from a 'shaperead' call.
Indeed, what I did: (1) I load a shape file of points (let's say 'myPoints'), with one field of interest, which is -let's say- 'ID'. (2) Once loaded, I add a new field 'Cluster' with a for loop:
for i=1:length(names)
try
myPoints([myPoints.ID]==str2double(names(i,:))).Cluster=T(i);
catch
disp(['No point for point n°',names(i,:)]) % This can happen
end
end
note that the array 'names' is the same length as 'T'. Each line in 'names' refers to the field 'ID' in the structure, and I join the corresponding 'T' value to the field 'Cluster'
(3) I prepare a List of clusters and desired colors for mapping:
ListOfClusters=unique(T);
ListOfColors=distinguishable_colors(length(ListOfClusters));
Until here, everything goes great: When I look at the structure 'myPoints', the right value of 'Cluster' is attached to the right row/observation.
Then comes the mapping and I thought building a loop, which plots all the points of a certain cluster with a specific predefined color ('ListOfColors'), then map the next cluster, etc.
figure
hold on
for i=1:length(ListOfClusters)
StyleOfPoints=makesymbolspec('Point',{'Default','Marker','o','MarkerFaceColor',ListOfColors(i,:),'MarkerEdgeColor',ListOfColors(i,:)});
MappedPoints(i)=mapshow(myPoints([myPoints(:).Cluster]==ListOfClusters(i)),'SymbolSpec',StyleOfPoints);
MappedPoints(i).DisplayName=num2str(ListOfClusters(i),'Cluster %d');
end
Here is the problem: The logical indexing:
[myPoints(:).Cluster]==ListOfClusters(i)
returns the wrong index for all loop iteration, and it is always shifted to the previous row(s) from the right one(s). For instance, Cluster value of 7 has only one observation at row 35, but the previous logical indexing returns a 1 in column 34 (This is how I first tracked the problem). This seems to be the same problem for all values of Cluster and I can not realise what I did wrong...
Does anyone have an idea? Thanks!
PS: I can send sample data if needed.
0 个评论
回答(1 个)
Mudambi Srivatsa
2017-6-27
I am surprised that the issue is with logical indexing. How about using "find" to get the index of the matching cluster ID as follows:
find([myPoints(:).Cluster] == ListOfClusters(i))
Also, it would be a good idea to verify the values being compared. I suggest putting a breakpoint to watch the values of [myPoints(:).Cluster] and ListOfClusters(i)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Multidimensional Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!