0
votes

I have a 1x500 cell of words that have variable length. For example:

words = {'SO','TODAY', 'IS', 'THURSDAY',...}

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:

startRow =  283             endRow = 309
            309                      332         
            332                      367
            ...                      ...

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.

This is my code so far:

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

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!

1
Well, 'SO' is actually 2 elements: 'S' and 'O'. A numeric matrix can only store one element in each position (actually it will store the ASCII value if you provide characters). Otherwise you can create a 1x3000 matrix of cells, and inside each cell you can store whatever you want. - Sembei Norimaki
shouldn't it be count(i)+1? - Dan
I can't figure out how to create a 1x3000 matrix of cells, all I seem to be able to do is create a cell of cells. - Kaly

1 Answers

2
votes

You can't fill a matrix with strings !

So you have to create a [3000x1] cell array.

mycell = cell(3000,1);

And you fill the cell with:

for i = 1:length(startRow)
    mycell(startRow(i):endRow(i)) = words(i);
end