MATLAB: Assignment has more non-singleton rhs dimensions than non-singleton subscripts

non-singletonsingletonsubscript

Hello, here is my code:
for f = 1:n ind(f,1) = num2cell(find(edges{f,1} == inde_x),2); in_d(f,1) = size(ind{f,:},2); indx = find(in_d ~= 0); end
I have edges matrix 9×1 cell and inde_x = 6
See the edges matrix below [6 9] [1 5] [5 6] [6 9] [6 7] [1 5] [5 9] 7 [1 7]
I'am trying to find values in inde_x = 6 in the edges matrix and ind matrix is formed as follows:
1 [] 2 1 1 [] [] 1 []
As seen above, in the edges matrix is equal to 6 in the row 6 finds the number and writes number of the index. If there is no value equal to 6, the [] sign is written but the number {8,1} does not have a value of 6, but the number 1 occurs and the following error is written:
Error in aaaaaaaaaaaaa (line 125) ind(f,1) = num2cell(find(edges{f,1} == inde_x),2);
How can I solve this problem? Thanks.

Best Answer

k = 6;
A = cellfun(@(edge) find(edge == k), edges, 'UniformOutput', false)
The loopy version of that:
k = 6;
%n unused. Never hardcode the number of elements. Ask matlab what it is with size or numel
A = cell(size(edges));
for idx = 1:numel(edges)
A{idx} = find(edges{idx} == k);
end
Note the difference between A(idx) and A{idx}. Your attempt at using num2cell show that you don't really know how to access cell arrays.