Add column with values to table based on value in existing column (look up)
조회 수: 5 (최근 30일)
이전 댓글 표시
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
채택된 답변
Stephen23
2025년 7월 29일
편집: Stephen23
2025년 7월 29일
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)
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!