user238469
user238469

Reputation:

How to make matrix with unequal length of the vectors in Matlab?

I have different distribution for different time (t) with each distribution having 10,000 elements. I have following line of code which computes CDF for different distributions inside the loop with t varying from 1 to nT:

[f_CDF(:,t),x_CDF(:,t)]=ecdf(uncon_noise_columndata_all_nModels_diff_t(:,1,t));

Matlab's function ecdf gives CDF values which could be less than the total number of elements in the distribution because the probability for the repeated elements gets added up. As a result, the output variables f_CDF and x_CDF run into problem of ??? Subscripted assignment dimension mismatch. error because of different length of vectors at different t.

How can I sort this problem such that NaN could fill up the places where the vector's length is less than the maximum length of any vector in the whole matrix and I am able to implement the above line of code inside the loop. Thanks.

Upvotes: 0

Views: 4237

Answers (1)

b3.
b3.

Reputation: 7175

Here are two of many ways to approach this problem:

1) Use a cell array

Consider storing the results in a cell array instead of a matrix which, by definition, requires columns to have the same length.

[f_CDF{t},x_CDF{t}]=ecdf(uncon_noise_columndata_all_nModels_diff_t(:,1,t));

2) Preallocate NaN matrices

Before running the loop which calculates the CDF results, create a matrix filled with NaNs. You know that each column won't be longer than 10,000 records.

f_CDF = NaN * ones(10000, nT);
x_CDF = NaN * ones(10000, nT);
for t = 1:nT
    [f_CDFTemp, x_CDFTemp]=ecdf(uncon_noise_columndata_all_nModels_diff_t(:,1,t));
    f_CDF(1:length(f_CDFTemp),t) = f_CDFTemp;
    x_CDF(1:length(x_CDFTemp),t) = x_CDFTemp;
end

Upvotes: 4

Related Questions