Funky Division Output and Mismatched Arrays

1 visualización (últimos 30 días)
kenny
kenny el 13 de Jun. de 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 comentario
Iain
Iain el 13 de Jun. de 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])

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 13 de Jun. de 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 comentario
kenny
kenny el 13 de Jun. de 2013
Editada: kenny el 13 de Jun. de 2013
oh yeah. thanks!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Sparse Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by