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

3 views (last 30 days)
Wesser on 27 Oct 2022
Commented: Jan on 28 Oct 2022
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 CommentsShow 1 older commentHide 1 older comment
Wesser on 27 Oct 2022
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 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 CommentsShow 1 older commentHide 1 older comment
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.

### Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

R2022a

### Community Treasure Hunt

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

Start Hunting!

Translated by