Matlab matrix with different Row Sizes

1.5k views Asked by At

I would like to create a matrix that has different row sizes but I'm not sure where to start.

I have a function that produces time signatures of 5000 photons. Not all those photons will produce meaningful time signatures. That function discards those signatures. I also iterate over this function 5000 times which means my column vector will have varying sizes on each iteration but never more than 5000.

function photon_FWHM = processFates(string,num)

for i = 1:num 
FATES = 'FATES';
str = int2str(i);
filename = strcat(FATES,str);
timestamp = Timestamp(filename);
if timestamp == 0
    continue;
end;
photon_timestamps(:,i) = timestamp;
end;

All in all I will have 5000 columns with some number of rows that vary with column. I'm not sure what a good solution is.

2

There are 2 answers

0
Jagte On BEST ANSWER

You could also try using cell arrays. Cell arrays may be a little slower, but they can do the job.

photon_timestamps{i} = vector;

then each element photon_timestamps{i} can be of different size.

also your block

if timestamp == 0
continue;
end;

needs a else part right?

0
thefourtheye On

I think matrices in MATLAB have to be of a rectangular shape (m x n). You could sub in non-values for NaN as a placeholder. The way that I usually do this is define the dimensions of the matrix in nested for loops and discard elements by placing a NaN.

for i = 1:rows
    for j = 1:5000
        if timestamp != 0
            yourmatrix(i, j) = timestamp;
        else
            yourmatrix(i, j) = NaN;
        end
    end
end

I don't completely understand all of your variables, so I can't put it directly in your function, but you can probably put it in there somewhere.