Nested For Loop Puzzle
显示 更早的评论
please can anyone help me solve this Mats lab assignment
Q.Nested for loop that can solve this puzzle
T H I S W A T S O A H G G G D T
thank you
5 个评论
Jan
2012-4-18
Dear Samuel, you forgot to mention what kind of result you are looking for. Do you want the characters to be sorted alphabetically?
And as for all other homework questions: Please post what you have tried so far and explain, which problems you have. Of course a forum is not a place where homework questions are solved such that you can submit a solution by cut&paste.
Samuel Awuah Botwe
2012-4-18
Thomas
2012-4-18
since this is an assignment we can only help you on the basis of what you have don so far.. and where you are getting stuck..
Jan
2012-4-18
The program is called "Matlab".
The question is not clear. There is no obvious way to "find this puzzle". And this does not concern the Matlab implementation, but it is simply not defined, what you should do with these 16 characters.
Samuel Awuah Botwe
2012-4-18
回答(3 个)
Sean de Wolski
2012-4-18
Golf:
reshape(['THISWATSOAHGGGDT'],4,4)'
Okay with for-loops:
for ii = 1
for jj = 1
m = reshape(['THISWATSOAHGGGDT'],4,4)';
end
end
3 个评论
Thomas
2012-4-18
Sean, they have to use nested for loops.. I guess thats an assignment requirement..
Sean de Wolski
2012-4-18
See the edit. Now with nested FOR-loops!
Thomas
2012-4-18
:) Now why did I not think about it.. I do not know how I would grade a student if they returned an answer like yours above..
Technically they used nested for loops.. :)
Thomas
2012-4-18
You are on the right track, to get alphabets..
try
%this is your input
a=['T' 'H' 'I' 'S' 'W' 'A' 'T' 'S' 'O' 'A' 'H' 'G' 'G' 'G' 'D' 'T']
% You need the two for loops.. that you have created above.. however you have to make sure you get the length of the loops rights..
P.S. Make sure you are not inputting your matrix in the output form itself.. the input should be an array as mentioned above.. and in your question.. You have to get it into 4x4 matrix using loops..
8 个评论
Samuel Awuah Botwe
2012-4-18
Thomas
2012-4-18
You need a new matrix of size 4x4
so your for loops must go from 1:4..
You are on the right track.. however l cannot be size(m) since your input is a, l can be size(a)
Samuel Awuah Botwe
2012-4-18
Thomas
2012-4-18
let me break it down for you
a=['T' 'H' 'I' 'S' 'W' 'A' 'T' 'S' 'O' 'A' 'H' 'G' 'G' 'G' 'D' 'T']
for i=1:4
m(i)=a(i);
end
m
This will give you the first line.. how do you go to the next line?
nested for loop.. get the next 4 alphabets.. how? increment the value in the loop get all the alphabets....
try a google search for 'matrix reshape nested for loop', you might get some basics..
This is a homework so I cannot give help with the code directly without causing you to cheat!!
Samuel Awuah Botwe
2012-4-18
Samuel Awuah Botwe
2012-4-18
Thomas
2012-4-18
yeah you need to input the next for loop as well.. how far is your code right now..
Thomas
2012-4-18
let me give you some pseudo code... to define the logic
input array; (1x16)
start first row (i) (for loop)
place first element of input array into first row (i) of new matrix (for loop j)
increment your row (i) place the next four alphabets
and do so till you get all your elements in the new matrix..
Samuel Awuah Botwe
2012-4-18
0 个投票
1 个评论
Jan
2012-4-19
Please note, that writing uppercase means shouting.
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!