Adding successive cells in a columns

조회 수: 1 (최근 30일)
Isaac Ahenkorah
Isaac Ahenkorah 2020년 9월 18일
답변: Isaac Ahenkorah 2020년 9월 18일
Hi,
I have a table (1x7921). I want to add successive columns. For example add column 1:16, 17:32, 32: 48 etc.
Is there a code that I can used to successively add specific range of columns?
  댓글 수: 4
Walter Roberson
Walter Roberson 2020년 9월 18일
Row 1 is not numeric. You then have 15 numeric rows after that that are to be added to give the yellow result. Then you add the next 16 entries to give the green result. Are you sure you want the yellow to have only 16 numeric entries added together? And as madhan points out, there would be one extra row left over, with a group ending at 7920 . Are you sure you want the last group to be only one number by itself?
It would make a lot more sense to discard that non-numeric first row and add groups of 16 after that.
Isaac Ahenkorah
Isaac Ahenkorah 2020년 9월 18일
Thanks Walter,
I have removed the non-numeric data in the first row. Also, I have filtered the data and I now have a total of 7920 rows, which will be divisible by 16.
Any suggestion on how to such each group of coloured data?

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

채택된 답변

Walter Roberson
Walter Roberson 2020년 9월 18일
sum(reshape(table2array(YourTable), 16, []))
but having a table object with only one row and 7921 variables is not at all common. It would be far more likely that you have a numeric vector, in which case
sum(reshape(YourVector, 16, []))
  댓글 수: 2
madhan ravi
madhan ravi 2020년 9월 18일
It would result in an error since 7921 is not divisible by 16.
Walter Roberson
Walter Roberson 2020년 9월 18일
Good point. But 7920 is divisible by 16, and the user shows a text entry in the first row, so it is plausibly 7920 rows of numeric values.

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

추가 답변 (2개)

madhan ravi
madhan ravi 2020년 9월 18일
편집: madhan ravi 2020년 9월 18일
Assuming after sir Walter’s comment:
T = TabLe{:, :};
n = numel(TabLe);
t = [TabLe, nan(1, 16 - mod(n, 16))];
Wanted = sum(reshape(t, [], 16), 'omitnan') % nansum() for older versions
  댓글 수: 2
Isaac Ahenkorah
Isaac Ahenkorah 2020년 9월 18일
Thanks Ravi,
I will try your suggession.
See below my data in excel format. I want to add each coloured column separately by just importing the data to Matlab and use a code that can loop around and add each successive column.
madhan ravi
madhan ravi 2020년 9월 18일
Both sir Walter’s answer and my answer should work , once you read the data using readtable()

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


Isaac Ahenkorah
Isaac Ahenkorah 2020년 9월 18일
Thanks Walter and Ravi,
I think both suggestions from you works really well. Much appreciated.
Regards,
Isaac

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by