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

1 次查看(过去 30 天)
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 个评论
Guillaume
Guillaume 2020-1-7
What defines a cluster? Adjacent values (orthogonal? diagonal?) between 0 and 10? If so, have you got the image processing toolbox?
Jonas Jeskulke
Jonas Jeskulke 2020-1-7
i dont have any toobox s far. I will try to explain: a cluster is formed if the values match ceartain criteria:
1. have to be between >0 and <=10
2. if it is in the same row
3. if it is in another row, but the row it is in is a a collum in an allready exsiting
"cluster matrix" (example: in the first row the second value meets the criteria.
so the values from the second row will be written in the same matrix)

请先登录,再进行评论。

采纳的回答

Guillaume
Guillaume 2020-1-7
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)
  1 个评论
Jonas Jeskulke
Jonas Jeskulke 2020-1-8
Thank you a lot thihs is working like a charm. i was stuck with that problem for a long time.
I appreciate the work you put into this.
Best regards Jonas J.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by