Funky Division Output and Mismatched Arrays

1 vue (au cours des 30 derniers jours)
kenny
kenny le 13 Juin 2013
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 commentaire
Iain
Iain le 13 Juin 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])

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 13 Juin 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
  1 commentaire
kenny
kenny le 13 Juin 2013
Modifié(e) : kenny le 13 Juin 2013
oh yeah. thanks!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Sparse Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by