how to add specific columns in table (in verse)

조회 수: 2 (최근 30일)
Daniel Cisek
Daniel Cisek 2022년 12월 7일
편집: Stephen23 2022년 12월 7일
I have a table T1 in cell that looks like this (values are double):
T1 =
Ligota1 Ligota2 Orzesze1 Orzesze2 Tychy1 Tychy2
_______ _______ ________ ________ _________ _________
1 2 3 4 5 6
7 8 9 1 2 3
4 5 6 7 8 9
0 1 2 3 4 5
6 7 8 9 0 10
How to add table rows omitting specific columns e.g. names begining with 'Ligota'. I tried to do it with varfun and rowfun functions with no luck. I was able to sum only the rows of all columns:
sum(T1{:,:},2);

채택된 답변

Jiri Hajek
Jiri Hajek 2022년 12월 7일
Hi,
you've righly spotted the subtle difference parentheses vs curly braces ("Curly braces, {}, returns an array concatenated from the contents of selected rows and variables.", see here). So it is sufficient to tell the summation operator, which columns to sum (let's say 2nd and 4th):
sum(T1{:,[2,4]},2);
  댓글 수: 4
Stephen23
Stephen23 2022년 12월 7일
편집: Stephen23 2022년 12월 7일
Although code golf is popular, for clarity and ease of debugging you could use two lines and logical indexing:
X = contains(T1.Properties.VariableNames,"Ligota");
S = sum(T1{:,~X,2)
Daniel Cisek
Daniel Cisek 2022년 12월 7일
Thanks Stephen23. I always try to compress code as much as possible. Maybe it's time to think about how to write it transparently.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by