Reputation: 303
I try to label 70 points (which can be seen as nodes) whose coordinates are contained in a matrix with two columns. In labelize I mean to appropriate to each one a number and for example to obtain a matrix with three columns where the first two are always the coordinates and the third the label
I saw in the doc labelnode
and graph
for example but I do not have the right version of matlab for these functions (I use R2015a). And labelmatrix
or bwlabel
transforms my matrix of coordinates into a matrix of 1.
How can I do ?
317 948
319 935
320 969
320 969
326 916
Upvotes: 0
Views: 454
Reputation: 1390
There are many ways to do this.
Simply use numeric array with numeric labels. You can easily append this to existing table.
arr(:,3) = labels;
You can use cell array, 1 and 2 would be numbers and 3 would be then string for label.
arrWL = cell(size(arr,1), 3);
arrWL{:,1:2} = arr;
arrWL{:,3} = labels;
You can also use table
to label rows and columns.
X = arr(:,1);
Y = arr(:,2);
arrWL = table(X, Y, 'RowNames', labels);
Put both data and labels together as separate entries in a cell array (with 2 elements).
arrWL = {arr, labels};
I could probably think of some other options if none of these are suitable.
Now, to generate unique numbers for labels, use:
[~,~,positions] = unique(arr, 'rows', 'stable');
% I prefer stable so the first point gets index 1 etc.
Upvotes: 1
Reputation: 1758
I am not entirely sure if I got your problem right. For what I understood, you can simply add a column of unique numbers to your data. If we say your data is stored in matrix X
, you can do:
labels = 1:size(X,1); % generate a vector of integers from 1 to number of rows of X
X = [X, labels']; % concatenate the matrix X and the vector of labels
% X = [ 317 948 1
% 319 935 2
% 320 969 3
% 320 969 4
% 326 916 5
% .........]
If instead you want the same label for points with the same coordinates, you can use unique
to generate those labels:
[~, ~, labels] = unique(X, 'rows');
X = [X, labels];
% X = [ 317 948 1
% 319 935 2
% 320 969 3
% 320 969 3
% 326 916 4
% .........]
Upvotes: 1