Comparing values in a table for each row
조회 수: 11 (최근 30일)
이전 댓글 표시
Hello, I'm new to Matlab and i don't really know how to efficiently solve this seemingly simple problem:
I have a really big bunch of data (89257x114 table) and for each row i want to compare the element of the 4th column with the elements from all columns between 14 and 113. My goal is to find the next higher value to the element in the 4th column. The next higher value for each row should then be stored inside another column (column 13 in the following example). I've already figured out how to do this with a for-loop, but it seems like it would take multiple days to execute. Here it is.
for i=1:89257
for z=1:100
if (M2{i,4} < M2{i,13+z})
M2{i,13} = M2{i,13+z}
end
end
end
I have already tried to use rowfun in combination with some anonymus functions i've created, but i just can't figure out a syntax that works.
I'm greatful for any kind of help!
댓글 수: 4
David Fletcher
2021년 5월 11일
편집: David Fletcher
2021년 5월 11일
Ignoring the business regarding function handles (if I'm honest, they have a tendency to bonzle my brain so I avoid them unless I have to absolutely use them) if you amend the min function to act on the full matrix by adding a dimension to work along i.e
temp=M2{:,14:113} - M2{:,4}
temp(temp<=0)=nan
[val,idx]=min(temp,[],2,'omitnan')
Wouldn't that return a set of indices that you can work back to the original table by adding a suitable offset so that you can index out the required values to save in your column? Or perhaps simpler yet, just add the values from column four back onto the min return values to recreate the original value and save that into your column
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!