How can thousands of matrix lines be written into a multidimensional cell-array?

1 次查看(过去 30 天)
I have created a cell array as follows:
YY = cell(31,10);
for l = 1:numel(YY)
YY{l} = zeros(1000,3);
end
YY has now 31 rows and 10 columns and each YY{i,j} has now 1000 rows and 3 columns.
The goal is now to fill the individual cells, with the elements of a matrix (which has some thousand lines and three columns).
I have tried the following to write the individual rows of the matrix into the respective cell:
% reading of simulated runoff (simulated)
S = 'runoff.txt'; % name of file (always the same)
D = 'C:\Users\heute\model\model_standalone\'; %pathname
d = dir(fullfile(D,'results*')); % file name is changing with consecuitive number
fmtS = ['%s',repmat('%f',1,6)]; % Format
opt = {'HeaderLines',1,'CollectOutput',true}; % headerline
L=length(d); % number of sub-folders
C=zeros(L,3); % preallocate
YY = cell(31,10); %create cell array
for l = 1:numel(YY) % create cells with a dimension of (1000,3)
YY{l} = zeros(1000,3);
end
for k = 1:L % loop to open files, make calculations and fill the cell arrays
fid = fopen(fullfile(D,d(k).name,S),'rt'); % open the files with simulated data
Z=textscan(fid,fmtS,opt{:}); % read simulated data
fclose(fid); % close file
dtS=Z{:,1}; % timestamp simulated (datetime)
Qs=Z{:,2}(:,6); % variable with simulated runoff
% Define timesteps, which are used for analysis
yr1=2014; yr2=2016;
ix=iswithin(dto,datenum(yr1,05,01),datenum(yr1,10,01)); % first year
for yr=yr1+1:yr2 % subsequent years
ix=ix | iswithin(dto,datenum(yr,05,01),datenum(yr,10,01));
end
f_1k = 1-cov(Qs(ix)-Qo(ix))/var(Qo(ix)); % NSE
f_2k = rms(Qs(ix)-Qo(ix)); % RMSE
f_3k = abs(mean(Qs(ix)-Qo(ix))); % BIAS
C(k,:) = [f_1k, f_2k, f_3k];
% write matrix C into the cell array
YY{k} = C;
end
With this variant, all individual cells are filled with the same values.
Another variant with a for - loop writes only one row in each cell. In this case four cells are filled with only one row.
for ii=1:4
YY(ii) = {C(ii, :)};
end
How can you solve the problem, that the first 1000 values are written in the first cell, the second 1000 values in the second cell etc?

采纳的回答

Walter Roberson
Walter Roberson 2017-6-17
Write them into a large numeric array, possibly a 4 dimension one. mat2cell() afterwards.
  7 个评论
Walter Roberson
Walter Roberson 2017-6-18
All of the material before has said that you have 31 rows of cells and each cell entry has 1000 numeric rows. That would be 31 * 1000 = 31000 total rows. But the error message is talking about 310000 total rows, which is 10 times larger. You need to figure out where the factor of 10 larger is coming from in your C, and perhaps decide if you want 310 rows of cells of 1000 rows each or if you want 31 rows of cells of 10000 rows each.
number_of_rows_of_cells = 31;
number_of_rows_in_each_cell = 1000;
number_of_columns_of_cells = 10;
number_of_columns_in_each_cell = 3;
YY = mat2cell(C, number_of_rows_in_each_cell * ones(1,number_of_rows_of_cells), number_of_columns_in_each_cell * ones(1,number_of_columns_of_cells));
You might notice that [310000 3] is simultaneously off by a factor of 10 in the number of columns: to have 10 columns of cells each with 3 columns, you need to have started with 30 columns, not with 3. This factor of 10 is the same as the factor you are off on the number of rows, suggesting that you have created rows where you wanted to create columns.
Depending on how you did the construction you just might be lucky enough to be able to use
YY = mat2cell( reshape(C, [], 30), 1000 * ones(1,31), 3 * ones(1,10));
but it is certainly possible that you will need to do a reshape() then a permute() then another reshape() in order to get the data into the proper locations to use mat2cell()
Glazio
Glazio 2017-6-19
@ Walter Roberson: It now seems that the desired results are provided, Thanks for your help.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 String 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by