Nested For Loop Puzzle
3 views (last 30 days)
Show older comments
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 Comments
Jan
on 18 Apr 2012
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.
Answers (3)
Sean de Wolski
on 18 Apr 2012
Golf:
reshape(['THISWATSOAHGGGDT'],4,4)'
Okay with for-loops:
for ii = 1
for jj = 1
m = reshape(['THISWATSOAHGGGDT'],4,4)';
end
end
3 Comments
Thomas
on 18 Apr 2012
:) 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
on 18 Apr 2012
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 Comments
Thomas
on 18 Apr 2012
yeah you need to input the next for loop as well.. how far is your code right now..
Thomas
on 18 Apr 2012
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..
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!