Help with for loops?
古いコメントを表示
I am trying to turn my switch construct into a for loop but I know I have to use it somewhere in my for loop. I am a beginner so I am trying to stick to simple commands as I do not understand much. I did recieve an answer before and I appreciate the help but I am just very frustrated I cannot figure this out. This is what I have so far:
...
MC_X='-..-'; MC_Y='-.--'; MC_Z='--..';
Word=input('Please enter text:','s');
Word=upper(Word);
for Index=['1' '2' '3' '4' '5' '6' '7' '8' '9' '0' ...
'A' 'B' 'C' 'D' 'E' 'F' 'G' 'H' 'I' 'J' 'K' 'L' 'M' ...
'N' 'O' 'P' 'Q' 'R' 'S' 'T' 'U' 'V' 'W' 'X' 'Y' 'Z'];
switch Word
case '1'
Code=MC_1;
case '2'
Code=MC_2;
...
case 'Z'
Code=MC_Z;
otherwise
disp('invalid input');
end
This is how I am supposed to be going about it, and I know it's not the fastest or simplest but this is how I understand it. Thank you to anyone that helps and to the person that tried to help me before.
--dpb edit took liberty of shortening case statement simply for brevity. Perhaps more folks will take some time when code isn't quite so long...--
2 件のコメント
dpb
2014 年 11 月 30 日
The above loop will simply repeat the same case for 37 times --
I recommend after searching for the previous referenced thread that you return to it and work through the explanation given there instead of starting another thread at the same place as before.
That is a very complete explanation and I don't see any sense in rehashing the same plowed ground -- any other response is going to be essentially the same as what has been provided to date.
For others' convenience, here's the preceding thread link--
Pam
2014 年 11 月 30 日
採用された回答
その他の回答 (1 件)
Adam
2014 年 11 月 30 日
If I were you I would use a map as follows:
keys = {'1'; '2'; '3';...
'X'; 'Y'; 'Z'};
values = {'.----'; '..---'; '...--';...
'-..-'; '-.--'; '--..'; }
morseCodeMap = containers.Map( keys, values );
I know you seem to be saying you want to do it the way you have above, but it is hard for someone to give help if you are only interested in doing it one way. A map is built for exactly this type of purpose and will solve your problem very neatly. Obviously above I abbreviated your alphabet, you would need to put all your morse code values in the keys and all your alphabet in the values, making sure they match up to each other in order.
Then you need no loops or huge switch statements, just the following:
Word=input('Please enter text:','s');
Word=upper(Word);
Code = morseCodeMap( Word );
4 件のコメント
Pam
2014 年 11 月 30 日
Well, it's your choice, but the best way to learn and understand something is to use it. There are help pages in the Matlab help with examples too.
What exactly are you trying to loop around though? Do you want to ask the user for many inputs or do you wish to loop around all the options just to find the one that matches what the user input?
Pam
2014 年 11 月 30 日
Guillaume
2014 年 11 月 30 日
Certainly, Pam, once you've sorted out your loop, you should consider Adam's answer. Your implemented a Look-Up Table (LUT). While a switch works, it's a lot of typing (and thus a higher risk of bugs). LUTs can be implemented using just plain arrays or, in this case, easily using a map.
カテゴリ
ヘルプ センター および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!