How to concatenate columns of varying lengths, generated by a for loop, into a matrix?
3 views (last 30 days)
Show older comments
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 Comments
David Hill
on 27 Oct 2022
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.
Accepted Answer
Jan
on 27 Oct 2022
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 Comments
Jan
on 28 Oct 2022
This is faster:
THETA_ObsNode(1:height(TEMP1), i) = TEMP1(:,3);
With your code you overwrite a lot of NaNs by NaNs.
More Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!