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 Commenti

Jan
Jan il 18 Apr 2012
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
Samuel Awuah Botwe il 18 Apr 2012
the questions was placed this way
please sir can you please help me solve this Mathslab assignment
Q.Nested for loop that can find this puzzle
T H I S
W A T S
O A H G
G G D T
Jan we are New students and we really dont know much about Matlab .was given us by a lecturer on first lecture day.please help me out.thanks
Thomas
Thomas il 18 Apr 2012
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
Jan il 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.
Samuel Awuah Botwe
Samuel Awuah Botwe il 18 Apr 2012
THIS WHAT I FIGURED OUT BUT GET AN ERROR CAN U PLEASE CHECK THE CODES FOR ME
m=[T,H,I,S;W,A,T,S;O,A,H,G;G,G,D,T]
l=size(m)
for i=1:l(1)
for j=1:l(2)
m(i,j) = m(i,j) * 2;
end
end
JUST AS THIS WORKS OUT AND DISPLAYS,HOW DO I MAKE IT DISPLAY IN ALPHABETS.....PLEASE TRY THIS CODE FOR ME....
m=[1,2,3,4;5,6,7,8;9,10,11,12]
l=size(m)
for i=1:l(1)
for j=1:l(2)
m(i,j) = m(i,j) * 2;
end
end

Accedi per commentare.

Risposte (3)

Sean de Wolski
Sean de Wolski il 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 Commenti

Thomas
Thomas il 18 Apr 2012
Sean, they have to use nested for loops.. I guess thats an assignment requirement..
Sean de Wolski
Sean de Wolski il 18 Apr 2012
See the edit. Now with nested FOR-loops!
Thomas
Thomas il 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.. :)

Accedi per commentare.

Thomas
Thomas il 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 Commenti

Samuel Awuah Botwe
Samuel Awuah Botwe il 18 Apr 2012
i tried this
a=['T' 'H' 'I' 'S' 'W' 'A' 'T' 'S' 'O' 'A' 'H' 'G' 'G' 'G' 'D' 'T']
l=size(m)
for i=[T,H,I,S;W,A,T,S;O,A,H,G;G,G,D,T]
for j=1:l(2)
m(i,j) = m(i,j) * 2;
end
end
but still gets errors,what am i getting wrong in the for loop
Thomas
Thomas il 18 Apr 2012
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
Samuel Awuah Botwe il 18 Apr 2012
I TRIED THIS AND THIS WAS THE ANSWER:
a=['T' 'H' 'I' 'S' 'W' 'A' 'T' 'S' 'O' 'A' 'H' 'G' 'G' 'G' 'D' 'T']
l=size(a)
for i=1:4(1)
for j=a:A(2)
m(i,j) = m(i,j) * 2;
end
end
a =
THISWATSOAHGGGDT
l =
1 16
ans =
1
HAVE ALSO TRIED THIS AND IT GIVES ME AN ERROR (Undefined function or variable 'T'.);
a=[T,H,I,S;W,A,T,S;O,A,H,G;G,G,D,T]
l=size(a)
for i=1:4(4)
end
end
I THINK YOUR ANSWER IS GETTING ME CLOSE BUT AM NOT GETTING SOMETHING RIGHT
Thomas
Thomas il 18 Apr 2012
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
Samuel Awuah Botwe il 18 Apr 2012
tHANKS ALOT,SO MUCH HELPFUL.GOD BLESS
Samuel Awuah Botwe
Samuel Awuah Botwe il 18 Apr 2012
ITS ACTUALLY HELPFULL BUT MY ANSWERS NEEDS TO BE DISPLAYED AS ;
T H I S
W A T S
O A H G
G G D T
Thomas
Thomas il 18 Apr 2012
yeah you need to input the next for loop as well.. how far is your code right now..
Thomas
Thomas il 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..

Accedi per commentare.

Samuel Awuah Botwe
Samuel Awuah Botwe il 18 Apr 2012

0 voti

AM STILL FIGURING OUT WHAT AM MISSING IN THE CODE WITH THE GREAT HELP GIVEN ME.SEAN's SOLUTION WORKED RIGHT BUT I GUESS IT HASNT GOT THE FOR LOOP..........AM SOO THANKFULL FOR ALL YOUR EFFORT......I CANT WAIT TO GET IT RIGHT BEFORE I LEAVE FOR LECTURES IN 20 MIN TIME SINCE I NEED TO PRESENT IT TODAY

1 Commento

Jan
Jan il 19 Apr 2012
Please note, that writing uppercase means shouting.

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by