MATLAB Answers

How do I extract some rows in a marix that satisfy a given condition?

1 view (last 30 days)
Hi everyone here my data:
  • a file text with the name of nodes (of a network) for example ('JUNCTION-0;JUNCTION1...') ordered by columns
  • a file.mat that contains the demands of nodes
First of all I want to group the two files in a matrix/data table and then extract only the rows for which the demand is >0. The final matrix should contain only the names of junctions with the corresponding demand that satisfy the given condition. Any suggestions? I attach the two files.
Thank you!
Stefania

  0 Comments

Sign in to comment.

Accepted Answer

Mario Malic
Mario Malic on 1 Nov 2020
Edited: Mario Malic on 2 Nov 2020
Hello,
clc;
clear;
Nodi = Read_File('nodi.txt');
load demand.mat
basedemand = basedemand'; % column orientation
basedemand(end+1:length(Nodi),1) = nan; % your data does not have equal num
% of rows, filling with nans
idx = basedemand>0;
Nodes = Nodi(idx);
Demand = basedemand(idx);
T = table(Nodes,Demand)
function File_Data = Read_File(Input_File_Path)
Temp_File_Data = fileread(Input_File_Path);
File_Data (:,1) = strsplit(Temp_File_Data, newline)'; % Column-wise file orientation
end
Your data does not have equal number of rows, some information might be missing, you should check the files.

  0 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!