필터 지우기
필터 지우기

How to modify selected rows of one variable in table

조회 수: 5 (최근 30일)
ThorBarra
ThorBarra 2021년 4월 15일
댓글: ThorBarra 2021년 4월 15일
Hi there,
Is there a way to code the following more elegantly? What this loop does is to
  1. select rows from a table based on some criteria
  2. then does some calculation on data in each row of that selection
  3. writes the result into a new variable in that table (in that specific row)
% ListNexafs is a table and here I select a set of rows that meet these
% criteria. tmpMatch is a logical array:
tmpMatch = (month(ListNexafs.lastModifiedDatenum) == 8) &...
(year(ListNexafs.lastModifiedDatenum) == 2020);
% for each line of the tabe
for ii = 1:size(ListNexafs,1)
% if that line meets the criteria above (logical is 1)
if tmpMatch(ii)
% I do some calculations. IzeroAugInCell(:,1) and (:,2) are doubles
% array.
ListNexafs.uhvI0{ii} = interp1(IzeroAugInCell(:,1),IzeroAugInCell(:,2),ListNexafs.xRayEkin_eV{ii});
end
end
This works. But its not so elegant. Could this be done with cellfun (even though, IzeroAugInCell are not cells)? Or is there another way to get rid of the nested for and if ? Thanks for any help, Thorsten
  댓글 수: 2
Matt J
Matt J 2021년 4월 15일
편집: Matt J 2021년 4월 15일
Is each cell ListNexafs.uhvI0{ii} vector-valued? And are the vectors contained in each cell of different lengths?
ThorBarra
ThorBarra 2021년 4월 15일
Yes, these fields are part of a larger table with cells containing vectors of different length.

댓글을 달려면 로그인하십시오.

채택된 답변

Matt J
Matt J 2021년 4월 15일
fun=@(c) interp1(IzeroAugInCell(:,1),IzeroAugInCell(:,2), c);
ListNexafs.uhvI0(tmpMatch) = cellfun(fun, ListNexafs.xRayEkin_eV(tmpMatch) ,'uni' , 0);
  댓글 수: 1
ThorBarra
ThorBarra 2021년 4월 15일
Works like a charme. Again, something learned! Merci.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by