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 ビュー (過去 30 日間)
Natalia Mehta
Natalia Mehta 2021 年 4 月 23 日
編集済み: Clayton Gotberg 2021 年 4 月 24 日
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
% have collected average values in volm
degm = (volm-co(2))/co(1);% making x the subject of y=mx+c
%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);
  4 件のコメント
Clayton Gotberg
Clayton Gotberg 2021 年 4 月 24 日
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.



Clayton Gotberg
Clayton Gotberg 2021 年 4 月 24 日
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 件のコメント
Clayton Gotberg
Clayton Gotberg 2021 年 4 月 24 日
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.


その他の回答 (0 件)




Community Treasure Hunt

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

Start Hunting!

Translated by