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

9 vues (au cours des 30 derniers jours)
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 commentaires
David Hill
David Hill le 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.
Wesser
Wesser le 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

Connectez-vous pour commenter.

Réponse acceptée

Jan
Jan le 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 commentaires
Wesser
Wesser le 27 Oct 2022
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 le 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Tags

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by