Equality error
조회 수: 3 (최근 30일)
이전 댓글 표시
function varargout=practice(varargin)
clc
idols(1).groupname = '2pm';
idols(1).nummembers = 1;
idols(1).gender = 'boys';
idols(1).debut = 2008;
idols(1).member(1).name = 'chansung';
idols(1).member(1).birthyear = 1990;
idols(1).member(1).height = 184;
idols(1).member(1).blood = 'B';
idols(2).groupname = 'SNSD';
idols(2).nummembers = 2;
idols(2).gender = 'girls';
idols(2).debut = 2007;
idols(2).member(1).name = 'taeyeon';
idols(2).member(1).birthyear = 1989;
idols(2).member(1).height = 'NA';
idols(2).member(1).blood = 'O';
idols(2).member(2).name = 'jessica';
idols(2).member(2).birthyear = 1989;
idols(2).member(2).height = 'NA';
idols(2).member(2).blood = 'B';
result=calc_avgage(idols, '2pm', 'SNSD');
disp(result)
end
function out=calc_avgage(idols, varargin)
n=0;
sum=0;
idols(2).member(1).birthyear
for i=1:nargin-1
idols2(i).groupname=char(varargin(i));
end
for i=1:length(idols2)
for j=1:length(idols)
a=idols2(i).groupname;
b=idols(j).groupname;
if a==b
for k=1:idols(j).nummembers
sum=sum+idols(j).member(k).birthyear;
n=n+1;
end
end
end
end
sum
n
out=sum/n;
end
If I run this code, why is it that when a==b, the code runs well
However when a~=b, the Matlab program gives the error
??? Error using ==> eq
Matrix dimensions must agree
How would I fix the problem?
댓글 수: 0
채택된 답변
Junaid
2011년 11월 27일
Dear for comparing the string you should use strcmp function.
ex.
a(1).name ='2pm'; a(2).name='SNSD'; a(3).name = '2pm';
strcmp(a(1).name , a(2).name ) % this will return 0
strcmp(a(1).name, a(3).name) % this will return 1.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Quantum Computing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!