Funky Division Output and Mismatched Arrays
조회 수: 1 (최근 30일)
이전 댓글 표시
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
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
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
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!