필터 지우기
필터 지우기

Funky Division Output and Mismatched Arrays

조회 수: 1 (최근 30일)
kenny
kenny 2013년 6월 13일
Hey, guys! i've been trying to solve this problem for the last few days and it's been driving me crazy. I had posted a few days ago and "Iain" was kind enough to offer advice but i still couldn't get the program to run properly. With some problems fixed, now i can't seem to get a normal answer.
All it has to do is divide the input of "units" and the corresponding "Values". "Values" is linked to the array "accepted" so that A = 1, B = 2, C = 3. so when I choose "A" and 3 units, I should get 0.33333 total A, but instead I get 0.0612
I feel that the problem lies in "index = find(strcmpi(elmnt,Accepted));" and "disp([(units) / Values {index} ])" I don't think it's properly matching the arrays
what obvious thing am i missing? thanks!
Accepted = {'A','B', 'C'};
Values = {'1','2','3'} ;
index = find(strcmpi(elmnt,Accepted));
elmnt = input('what letter? ','s');
if any(strcmpi(elmnt,Accepted))
disp (' \n')
else
disp ('that''s not going to work')
end
units = input ('how many units of it?');
disp([((units) / Values {index} )])
disp('units of')
disp(elmnt )
  댓글 수: 1
Iain
Iain 2013년 6월 13일
Accepted = {'A','B', 'C'};
Values = [1 2 3] ;
elmnt = input('what letter? ','s');
index = find(strcmpi(elmnt,Accepted));
Value = Values(index);
if index %a sneaky shorthand
disp (' \n')
else
disp ('that''s not going to work')
end
units = input ('how many units of it?');
disp([ num2str(units/Value) ' units of ' elmnt])

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

채택된 답변

Walter Roberson
Walter Roberson 2013년 6월 13일
Values = {'1','2','3'} ;
defines Values in terms of character. You are not dividing by 3, you are dividing by '3'
>> '3'+0
ans =
51
>> char(51)
ans =
3
>> 51/3
ans =
17
>> 51/'3'
ans =
1
  댓글 수: 1
kenny
kenny 2013년 6월 13일
편집: kenny 2013년 6월 13일
oh yeah. thanks!

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

추가 답변 (0개)

카테고리

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