Add column with values to table based on value in existing column (look up)
7 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I want to add a column to the end of Matrix.csv (my actual data is irregular and 18000+ lines) that matches the value in the Direction column to the heading below and outputs the corresponding wDir value.
I am trying to produce something like an excel lookup function after creating headingTable through the below code.
heading = ["000", "015", "030", "045", "060", "075", "090", "105", "120", "135", "150", "165", ...
"180", "195", "210", "225", "240", "255", "270", "285", "300", "315", "330", "345"];
wDir = [90 75 60 45 30 15 0 345 330 315 300 295 270 255 240 225 210 195 180 165 150 135 120 105];
count = 0;
for i =1:numel(wDir)
heading1 = heading(i);
wDir1 = wDir(i);
outData = [heading1 wDir1];
count =count + 1;
headingTable(count,:) = outData;
end
0 commentaires
Réponse acceptée
Stephen23
le 29 Juil 2025
Modifié(e) : Stephen23
le 29 Juil 2025
This is MATLAB so forget about Excel and loops.
Use e.g. ISMEMBER or DISCRETIZE:
T = readtable('Matrix.csv')
H = [0,15,30,45,60,75,90,105,120,135,150,165,180,195,210,225,240,255,270,285,300,315,330,345,360];
W = [90,75,60,45,30,15,0,345,330,315,300,295,270,255,240,225,210,195,180,165,150,135,120,105];
T.WDir = discretize(T.Direction,H,W)
Even better would be to avoid those fiddly vectors and functions entirely:
T.Wsimple = mod(90-T.Direction,360)
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Matrix Indexing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!