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 天)
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 个评论

请先登录,再进行评论。

采纳的回答

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 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by