sum of range of matrix elements from starting and end point

9 ビュー (過去 30 日間)
Abhijit Sardar
Abhijit Sardar 2021 年 1 月 1 日
コメント済み: KALYAN ACHARJYA 2021 年 1 月 3 日
I have 90000*20 matrix i want to add 50 elements of 18th column successively. how can i do so please help.also could you please tell how to do sum of elements in the vector if the starting and end point is known.

採用された回答

KALYAN ACHARJYA
KALYAN ACHARJYA 2021 年 1 月 1 日
編集済み: KALYAN ACHARJYA 2021 年 1 月 1 日
Lets say the matrix data is variable name as "mat"
result=sum(mat(1:50,18))
"could you please tell how to do sum of elements in the vector if the starting and end point is known".
If 1-D vector, you can use sum directly
result=sum(mat(Start_index:End_index))
For 2 D case
data=mat(From rows:Till Rows number,From columns:Till Columns number)
result=sum(data(:))
  4 件のコメント
Abhijit Sardar
Abhijit Sardar 2021 年 1 月 3 日
No, i meant first i want sum of 1 - 50 elements and then 2-51,3-52 like so on... i am coding like this but in 'res' i am only getting one answer. i want the consecutive sum of 50 elements correspoing to all 90000 row elements.
for i = 1:p-50
res = sum(m(i:i+50,18))
end
KALYAN ACHARJYA
KALYAN ACHARJYA 2021 年 1 月 3 日
In that case you have to use array indexing
l=1;
res=zeros(1,,,,expected_num_of interation); % Must do
for i = 1:p-50
res(l)= sum(m(i:i+50,18))
end
In this case you can avoid loop, please use movsum, as follows (Recommended)
data=m(:,18);
sum_result=movsum(data,50)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by