Assignment has more non-singleton rhs dimensions than non-singleton subscripts?

조회 수: 1 (최근 30일)
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 9x1 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.
  댓글 수: 3
Abdullah Türk
Abdullah Türk 2018년 10월 22일
Thank you for your warnings, Jan.
k = 6;
n = 9;
for f = 1:n
A(f,1) = num2cell(find(edges{f,1} == k),2);
end
I have matrix 9x1 named edges.
  • edges{1,1} = [6 9]
  • edges{2,1} = [1 5]
  • edges{3,1} = [5 6]
  • edges{4,1} = [6 9]
  • edges{5,1} = [6 7]
  • edges{6,1} = [1 5]
  • edges{7,1} = [5 9]
  • edges{8,1} = 7
  • edges{9,1} = [1 7]
I want to find subscripts equal to k in the edges matrix and a A matrix has occurred as a result of the loop:
  • A{1,1} = 1
  • A{2,1} = []
  • A{3,1} = 2
  • A{4,1} = 1
  • A{5,1} = 1
  • A{6,1} = []
  • A{7,1} = []
  • A{8,1} = 1
  • A{9,1} = []
and result of the loop:
Assignment has more non-singleton rhs dimensions than non-singleton subscripts
Error in aaaaaaaaaaaaa (line 125)
ind(f,1) = num2cell(find(edges{f,1} == inde_x),2);
I had to get A {8.1} = [] according to the edges matrix I have, but A {8.1} = 1. Values other than A {8.1} appear to be correct.
Thank you from now.
Guillaume
Guillaume 2018년 10월 22일
I was going to ask why you'd use a cell array instead of a Nx2 matrix to store edges then saw that one of your edge has only one element. Why? Don't your edge describe a graph? If so all edges should have two elements. If it's a self-loop, the node should be repeated.

댓글을 달려면 로그인하십시오.

채택된 답변

Guillaume
Guillaume 2018년 10월 22일
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.
  댓글 수: 1
Abdullah Türk
Abdullah Türk 2018년 10월 23일
Thanks Guillaume. Problem solved. I'm new in Matlab. I know the difference between A(idx) and A{idx} but after seeing your loop, I realized that my error was in the use of num2cell. Thank you again.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

KSSV
KSSV 2018년 10월 22일
k = 6;
n = 9;
edges{1,1} = [6 9]
edges{2,1} = [1 5]
edges{3,1} = [5 6]
edges{4,1} = [6 9]
edges{5,1} = [6 7]
edges{6,1} = [1 5]
edges{7,1} = [5 9]
edges{8,1} = 7
edges{9,1} = [1 7]
A = cell(n,1) ;
for f = 1:n
T = num2cell(find(edges{f,1} == k),2) ;
if ~isempty(T)
A(f,1) = T;
else
A(f,1) = {[]};
end
end

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by