How to concatenate columns of varying lengths, generated by a for loop, into a matrix?

4 次查看(过去 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
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.
Wesser
Wesser 2022-10-27
I fond this function that can pad a vector with NAN or zeros...but is there another way to do this? Would I call the function after
num_sim = 1000; %1000 monte carlo simulations
%~~~~~Preallocation~~~~~~~~~~`
%Obs_Node.out
Node_CONC=zeros(200000,num_sim);
THETA_ObsNode = zeros(200000,num_sim);
FLUX_ObsNode = zeros(200000,num_sim);
CONC_ObsNode = zeros(200000,num_sim);
%~~~~~~~~~~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
%pad theta_ObsNode with NAN to 200000, but how?
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
%pad flux_ObsNode with NAN to 200000, but how?
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
%pad Conc_ObsNode with NAN to 200000, but how?
CONC_ObsNode(:,i) = Conc_ObsNode(:); %add column from this loop to past loops to make matrix of data
fclose(Obs_Node);
end

请先登录,再进行评论。

采纳的回答

Jan
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 个评论
Wesser
Wesser 2022-10-27
This set me on the right track. Thanks to all for the continued guidance! This is what I ended up with:
%~~~~~Preallocation~~~~~~~~~~`
%Obs_Node.out
THETA_ObsNode = nan(300000,num_sim);
%~~~~~~~~~~Coalesce data from Obs_Node.out files~~~~~~~~~~~~
for i=1:num_sim
Obs_Node = fopen("/Users/apple//Simulations/MC_"+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); % Hydraulic Conductivity
theta_ObsNode(end+1:300000)=nan;
THETA_ObsNode(:,i) = theta_ObsNode(:);
fclose(Obs_Node);
end
Jan
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 CenterFile Exchange 中查找有关 Sources 的更多信息

标签

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by