Unable to perform assignment because the left and right sides have a different number of elements.
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Jonas Jeskulke
le 7 Jan 2020
Commenté : Jonas Jeskulke
le 8 Jan 2020
I have a problem with the attached script sectrion. anr is a 46x46 double Matrix, with calculated values. I cant find my error but it probably is a very simple one.
Thx for the Help in advance.
10 commentaires
Guillaume
le 7 Jan 2020
What defines a cluster? Adjacent values (orthogonal? diagonal?) between 0 and 10? If so, have you got the image processing toolbox?
Réponse acceptée
Guillaume
le 7 Jan 2020
Your criteria for the cluster connectivity is a bit strange in my opinion since it doesn't actually require adjacency. If that's what you want I think the easiest may be to build a graph of connectivity and let matlab split the graph into its components. No toolbox needed for that.
I've tested with the following matrix:
inrange = [1 0 0 1 0 0 1 0 0;
1 0 1 0 0 0 0 0 0;
0 1 0 0 1 1 0 0 0;
0 0 0 0 0 0 1 1 0;
0 0 0 0 0 0 0 0 1;
0 1 0 0 1 0 0 0 0];
which according to your criteria has 3 clusters (elements of rows 1, 2, 4 are one cluster, elements of rows 3 and 5 another, and the element of row 6 is another). In your code,
irange = anr > 0 & anr < 10;
With that:
[row, col] = find(inrange); %locate elements in range
%now build connectivity graph. First iterate over the points to find which points share the same column or row
connectto = arrayfun(@(idx) find(row(idx) == row | col(idx) == col), 1:numel(row), 'UniformOutput', false); %Note that this will create self-loop as a point obviously has the same column and row as itself. Self-loops don't matter for connectivity
%convert the above in a 2 column matrix of start and end nodes
connectivity = [repelem((1:numel(row))', cellfun(@numel, connectto)), vertcat(connectto{:})];
%build graph
g = graph(connectivity(:, 1), connectivity(:, 2));
%optionally plot it
plot(g);
%get connected components of the graph, i.e. a cluster by the given criteria of sharing a row or column
clusters = conncomp(g, 'OutputForm', 'cell');
%replace node indices by [row, col] coordinates
clusters = cellfun(@(idx) [row(idx), col(idx)], clusters, 'UniformOutput', false);
%for display:
celldisp(clusters)
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!