How can fix this error ?

조회 수: 1 (최근 30일)
yaznp aldhyani
yaznp aldhyani 2023년 5월 10일
편집: Nathan Hardenberg 2023년 6월 5일
we have run this code and we have obtained this error "
Operator '==' is not supported for operands of type 'cell'.
names = {'1' '2' '3' '4' '5' '6' '7' '8' 'A' 'B' 'C'};
A1=[1 0 0 0 5 0 0 5 0 0 0; ...
0 1 0 0 0 0 5 5 0 0 0; ...
0 0 1 10 5 0 0 0 0 0 0;...
0 0 10 1 0 0 3 0 0 0 0;...
5 0 5 0 1 0 0 0 1 0 0; ...
0 0 0 0 0 1 0 5 0 1 0;...
0 5 0 3 0 0 1 0 0 0 1;...
5 5 0 0 0 5 0 1 0 0 0;...
0 0 0 0 1 0 0 0 1 0 0;...
0 0 0 0 0 1 0 0 0 1 0;... 0 0 0 0 0 0 1 0 0 0 1];
num_cars = zeros(3,3); % initialize matrix to store number of cars passing
for i=1:3 % loop through rows of adjacency matrix
for j=1:3 % loop through columns of adjacency matrix
if i~=j % ignore diagonal entries (no cars can pass from a node to itself)
num_cars(i,j) = sum(names(A1(i,:)~=0)==names(A1(j,:)~=0)); % count number of cars passing between nodes i and j
end
end
end
This is error "
Operator '==' is not supported for operands of type 'cell'

답변 (1개)

Nathan Hardenberg
Nathan Hardenberg 2023년 5월 25일
편집: Nathan Hardenberg 2023년 6월 5일
This error occurs when you want to compare to cells, which you can't do. What you probably want is to compare the contents of the cells. For this put the corresponding index after the cell:
X = {'a', 'b'};
Y = {'a', 'c'};
X{1} == Y{1} % compare 'a' to 'a' => works
ans = logical
1
X{2} == Y{2} % compare 'b' to 'c' => works
ans = logical
0
X == Y % this throws the error
Operator '==' is not supported for operands of type 'cell'.

카테고리

Help CenterFile Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by