14 views (last 30 days)

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.

Guillaume
on 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)

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 10 Comments

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/499241-unable-to-perform-assignment-because-the-left-and-right-sides-have-a-different-number-of-elements#comment_783708

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/499241-unable-to-perform-assignment-because-the-left-and-right-sides-have-a-different-number-of-elements#comment_783708

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/499241-unable-to-perform-assignment-because-the-left-and-right-sides-have-a-different-number-of-elements#comment_783709

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/499241-unable-to-perform-assignment-because-the-left-and-right-sides-have-a-different-number-of-elements#comment_783709

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/499241-unable-to-perform-assignment-because-the-left-and-right-sides-have-a-different-number-of-elements#comment_783710

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/499241-unable-to-perform-assignment-because-the-left-and-right-sides-have-a-different-number-of-elements#comment_783710

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/499241-unable-to-perform-assignment-because-the-left-and-right-sides-have-a-different-number-of-elements#comment_783714

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/499241-unable-to-perform-assignment-because-the-left-and-right-sides-have-a-different-number-of-elements#comment_783714

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/499241-unable-to-perform-assignment-because-the-left-and-right-sides-have-a-different-number-of-elements#comment_783716

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/499241-unable-to-perform-assignment-because-the-left-and-right-sides-have-a-different-number-of-elements#comment_783716

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/499241-unable-to-perform-assignment-because-the-left-and-right-sides-have-a-different-number-of-elements#comment_783724

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/499241-unable-to-perform-assignment-because-the-left-and-right-sides-have-a-different-number-of-elements#comment_783724

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/499241-unable-to-perform-assignment-because-the-left-and-right-sides-have-a-different-number-of-elements#comment_783727

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/499241-unable-to-perform-assignment-because-the-left-and-right-sides-have-a-different-number-of-elements#comment_783727

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/499241-unable-to-perform-assignment-because-the-left-and-right-sides-have-a-different-number-of-elements#comment_783733

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/499241-unable-to-perform-assignment-because-the-left-and-right-sides-have-a-different-number-of-elements#comment_783733

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/499241-unable-to-perform-assignment-because-the-left-and-right-sides-have-a-different-number-of-elements#comment_783735

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/499241-unable-to-perform-assignment-because-the-left-and-right-sides-have-a-different-number-of-elements#comment_783735

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/499241-unable-to-perform-assignment-because-the-left-and-right-sides-have-a-different-number-of-elements#comment_783741

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/499241-unable-to-perform-assignment-because-the-left-and-right-sides-have-a-different-number-of-elements#comment_783741

Sign in to comment.