Matlab: repeat string elements N times
<p>I have a 1x500 cell of words that have variable length. For example:</p>

<pre><code>words = {'SO','TODAY', 'IS', 'THURSDAY',...}
</code></pre>

<p>In addition, I have two 500x1 arrays of integers, the first contains numbers referring to the starting row, the second contains numbers referring to the end row. For example:</p>

<pre><code>startRow =  283             endRow = 309
            309                      332         
            332                      367
            ...                      ...
</code></pre>

<p>What I need to do is, I need to fill a 1x3000 matrix of zeros from row 283-308 with the word 'SO' (so that the word is in every row), from row 309-331 with the word 'TODAY', from 332-367 with the word 'IS' and so on until the last element of endRow has been processed.</p>

<p>This is my code so far:</p>

<pre><code>new_val = zeros(1,3000);

for t = 1:size(startRow,1) 
    count(1,t) = endRow(t)-startRow(t);
    word{t} = words{t};
end

for i = 1:size(startRow,1)
    new_val(1,startRow(i):endRow(i)-1) = repmat(word{1,i},count(i),1);
end
</code></pre>

<p>The problem occurs in the second loop because assignment dimensions mismatch. The size of the left hand element after one iteration is [1,26], the size of the right hand element is [26,2] after one iteration because apparently 'SO' is counted as 2 elements. I don't know what to do about this problem, so any suggestions, hints and tips are appreciated very much. Thank you!</p>