Funky Division Output and Mismatched Arrays
1 view (last 30 days)
Show older comments
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 Comment
Iain
on 13 Jun 2013
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])
Accepted Answer
Walter Roberson
on 13 Jun 2013
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
More Answers (0)
See Also
Categories
Find more on Sparse Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!