Why is my code only writing to the array 4 times?

word = input("Type a scrambled string: ", 's');
len = length(word)
totalPoss = factorial(len)
divisions = totalPoss/len
space = zeros(totalPoss,len);
%disp(space);
for i = 1:len
for j = 1:divisions
space((j + len*(i-1)),1) = word(i);
end
end
disp(space);
When I run the above code in a Linux environment using octave, I expect to see the array space's firec column to be filled with the letters of the input, word.
For example, if the input were to be "lime," I would expect that space[1:6][1] to be "l," space[7:12][1] to be "i," etc. Instead, this is the output I am getting:
Type a scrambled string: lime
len = 4
totalPoss = 24
divisions = 6
108 0 0 0
108 0 0 0
108 0 0 0
108 0 0 0
105 0 0 0
105 0 0 0
105 0 0 0
105 0 0 0
109 0 0 0
109 0 0 0
109 0 0 0
109 0 0 0
101 0 0 0
101 0 0 0
101 0 0 0
101 0 0 0
101 0 0 0
101 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
I am unsure as to why the first three characters are only being inserted 4 times and why all of the extra spaces are at the bottom of the array. (I also don't know why the letters are ascii numbers, but that is a different problem I think).

 Accepted Answer

You defined space as a double array:
space = zeros(totalPoss,len);
That is why it is printing as ASCII values. Try this instead:
space = repmat(' ',totalPoss,len);

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 12 Jul 2016

Edited:

on 12 Jul 2016

Community Treasure Hunt

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

Start Hunting!