my for loop doesn't seem to be working. I want to add the correct letter to a string based on the equality of two arrays. How can I make this wok?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Natalia Mehta
le 23 Avr 2021
Modifié(e) : Clayton Gotberg
le 24 Avr 2021
dec = table2array(decodeMessage);
ix = -40:3:38;%collating the degrees corresponding to the letters
message = "";
al = "abcdefghijklmnopqrstuvwxyz ";%corresponding letter array
volm = zeros(1,16);
in = 1;
for j=1:1:16
volm(j) = mean(dec([in:in+14],2));
in = in+15
end
% have collected average values in volm
degm = (volm-co(2))/co(1);% making x the subject of y=mx+c
round(degm);
%rounded to the nearest integer, now we have our final values
%% this is where it stops working, how do i fix this? the message string is blank
for k = 1:16
for o = 1:27
if ix(o) == degm(k)
message = message+ al(o);
else
continue
end
end
end
4 commentaires
Clayton Gotberg
le 24 Avr 2021
Modifié(e) : Clayton Gotberg
le 24 Avr 2021
I can safely say we've all been there! After all, I missed it in my first reading too. I'm glad I could help.
Réponse acceptée
Clayton Gotberg
le 24 Avr 2021
It seems the problem is that al is a string and therefore counts as one object for MATLAB. You can remedy this by switching to a char array (using single quotes instead of double quotes) or by splitting the string into an array of strings.
single_string = "abcdefghijklmnopqrstuvwxyz "; % 1x1 object so can't be indexed
% Solutions:
string_array = ["a" "b" "c" "d" "e" ... ]; % And so on
% or
char_array = char("abcdefghijklmnopqrstuvwxyz ");
% or
char_array = 'abcdefghijklmnopqrstuvwxyz ';
I bet your error was something like "Index exceeds array bounds". Please make sure you include any errors when you make your post so we can help you as much as possible!
4 commentaires
Clayton Gotberg
le 24 Avr 2021
That makes sense. If after all of this you're still not getting an output, can you attach the code or a workspace after you run the code to a comment? I can dig a little deeper into the variables and see if there's something else causing the problem. My gut instinct is that there's a problem with the if ix(o) == degm(k) part of the code.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements 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!