How to concatenate columns of varying lengths, generated by a for loop, into a matrix?
1 次查看(过去 30 天)
显示 更早的评论
How to concatenate columns vectors of varying lengths, generated by a for loop, into a matrix?
num_sim = 1000; %1000 monte carlo simulations
%~~~~~~~~~~Coalesce data from Obs_Node.out files~~~~~~~~~~~~
for i=1:num_sim
Obs_Node = fopen(["/Users/apple/Dropbox/My Mac (apple’s MacBook Pro)/Desktop/Simulations/MC_"+num2str(i)+'/Obs_Node.out']); % Open monte carlo output file in Path (i)
skip_lines=11; %skip all the lines until the output data of interest
for k=1:(skip_lines)
x=fgetl(Obs_Node);
end
temp1 = fscanf(Obs_Node,'%f',[5,Inf]); %scan the matrix of data
TEMP1 = temp1'; % transpose data
theta_ObsNode = TEMP1(:,3); % Isolate the Hydraulic Conductivity column
THETA_ObsNode(:,i) = theta_ObsNode(:); %add column from this loop to past loops to make matrix of data
flux_ObsNode = TEMP1(:,4); % Isolate the Water Flux column
FLUX_ObsNode(:,i) = flux_ObsNode(:); %add column from this loop to past loops to make matrix of data
Conc_ObsNode = TEMP1(:,5); % Isolate the Concentration g/cm3 column
CONC_ObsNode(:,i) = Conc_ObsNode(:); %add column from this loop to past loops to make matrix of data
fclose(Obs_Node);
end
2 个评论
David Hill
2022-10-27
You will have to pad the columns with zeros or nan to make them the same length or you will have to use a cell array.
采纳的回答
Jan
2022-10-27
Maybe all you need is:
Node_CONC = nan(200000,num_sim);
THETA_ObsNode = nan(200000,num_sim);
FLUX_ObsNode = nan(200000,num_sim);
CONC_ObsNode = nan(200000,num_sim);
...
TEMP1 = temp1'; % transpose data
s = size(TEMP1, 1);
THETA_ObsNode(1:s, i) = TEMP1(:,3);
FLUX_ObsNode(1:s, i) = TEMP1(:,4);
CONC_ObsNode(1:s, i) = TEMP1(:,5);
2 个评论
Jan
2022-10-28
This is faster:
THETA_ObsNode(1:height(TEMP1), i) = TEMP1(:,3);
With your code you overwrite a lot of NaNs by NaNs.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!