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
Natalia Mehta
Natalia Mehta 2021년 4월 24일
Thank you so much! Turns out that it was because i didn't save degm to a variable, my bad ahah
Clayton Gotberg
Clayton Gotberg 2021년 4월 24일
편집: 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
Natalia Mehta
Natalia Mehta 2021년 4월 24일
My apologies for not including the value of co(), it's something i used earlier in the program.
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