MATLAB Answers

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

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.

  10 Comments

Show 7 older comments
Jonas Jeskulke
Jonas Jeskulke on 7 Jan 2020
Ok sure thing. as final output i want to have "clusters" of data. as example: i have a matrix looking like this as input (anr):
0 6.8 6.2 12.4 96.3
0 0 4.9 12.3 89.9
0 0 0 11.1 90.6
0 0 0 0 7.5
0 0 0 0 0
so from this input i want ot get Matrixes that look like this
1 2
1 3
2 3
and a second on with
4 5
so i have a cluster in the first matrix a cluster in the second but they are not intertwinde witch each other. does this make it a bit clearer?
Guillaume
Guillaume on 7 Jan 2020
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 on 7 Jan 2020
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)

Sign in to comment.

Accepted Answer

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

  1 Comment

Jonas Jeskulke
Jonas Jeskulke on 8 Jan 2020
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.

Sign in to comment.

More Answers (0)

Sign in to answer this question.

Products


Release

R2019a