MATLAB: Unable to perform assignment because the left and right sides have a different number of elements.

MATLABunable to perform assignment

I have a problem with the attached script sectrion. anr is a 46×46 double Matrix, with calculated values. I cant find my error but it probably is a very simple one.
Thx for the Help in advance.

Best Answer

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)