I want to sum up specific array in a matrix
이전 댓글 표시
For example If I have a matrix like this
x = [ 2.356 37
1.351 39
4.394 37
3.622 39
4.489 39
3.135 37
6.653 39 ]
I want to sum up the values that are specific to 37 and 39 seperatly only if it is alternative. If 39 and 37 are not alternative like in 4th and 5 th row wants to add that to the next subsecuent value in this case it is 37.
Some of the 37 should be (2.356+4.394+4.489+3.135), 39 should be (1.351+3.622+6.653). Please help in getting a matlab code for this!
Thank you in advance
댓글 수: 3
Andrei Bobrov
2019년 6월 22일
x = [ 2.356 37
1.351 39
4.394 37
3.622 39
4.489 39
3.135 37
3.5147 37
3.6058 37
2.827 37
3.6134 37
6.653 39];
result - ??
Bhagya Lakshmi Marella
2019년 6월 22일
Bhagya Lakshmi Marella
2019년 6월 24일
채택된 답변
추가 답변 (1개)
KALYAN ACHARJYA
2019년 6월 22일
편집: KALYAN ACHARJYA
2019년 6월 22일
x =[2.356 37
1.351 39
4.394 37
3.622 39
4.489 39
3.135 37
6.653 39]
idx=(find(x(:,2)==37))';
sum_37=0;
for i=1:length(idx)
sum_37=sum_37+x(idx(i),1);
end
% You can do the same for 39 too
Wait I am providing the same without loop
idx=(find(x(:,2)==37))';
sum_37=sum(x(idx(:),1));
For 39:
idx=(find(x(:,2)==39))';
sum_39=sum(x(idx(:),1));
댓글 수: 2
Bhagya Lakshmi Marella
2019년 6월 22일
KALYAN ACHARJYA
2019년 6월 24일
You can do that using 1/2 line of logical statments and index comparision.
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!