Reputation: 119
I have included a link to a picture of the solution to this problem. https://i.sstatic.net/nUdZn.png
Can someone explain what is going in the fist line of code C=char(100*ones(4,5))
I understand that we are preallocating a matrix by doing this, but I don't understand why we are including 100*ones
. Why wouldn't the matrix be allocated correctly with just char(4,5)
, thereby preallocating a m4x5 matrix with strings as inputs, not doubles?
Many thanks in advance - I'm brand new to programming and learning MATLAB as my first language and platform.
Upvotes: 0
Views: 67
Reputation: 89
Here is what C=char(100*ones(4,5))
does in steps.
1)A ones matrix of 4 x 5 is created.
2)This matrix is then multiplied by 100.
3)Then a character matrix is generated from the resulting matrix in step 2. The char
function converts integers to their respective ASCII characters. Please look at the ascii table. The first few characters are non alphabetical characters, hence the author thought to initialize the character matrix with d
, i.e (100 * 1) which is d
in ascii.
You can't do char(4,5)
because the char function takes a array/matrix of integers to convert to character matrix.
Upvotes: 0
Reputation: 18299
1) Create 4x5 matrix of ones
2) multiply it by 100, thus creating 4x5 matrix of 100s
3) char() to convert it to matrix of characters whose ASCII code is 100 (which is 'd').
The result is 4x5 matrix of 'd's.
Upvotes: 1