필터 지우기
필터 지우기

Obatin the sum from reading columns from 2 matrices

조회 수: 1 (최근 30일)
Damith
Damith 2014년 6월 17일
답변: the cyclist 2014년 6월 17일
Hi,
I have matrix A=(5479 x 378) and RowID matrix (15 x 2). I need to get the sum for each column of A for RowIDs 1:15
for id=1:378
for ix=1:1:15
St(ix,1)=sum(new(:,id)([RowID(ix,1):RowID(ix,2)],:));
end
end
Error: ()-indexing must appear last in an index expression.
This is the error I am getting when I run the above code. Can somebody help me to fix that please?
Thanks in advance.
  댓글 수: 3
Damith
Damith 2014년 6월 17일
편집: Damith 2014년 6월 17일
Thanks. Here is an simple example. I need to get the sum of first and second row of new matrix for all columns in new refering to the row numbers from RowID matrix. Hope you understand now. please let me know if you need more clarification.
I know there is some error in my coding so I need to fix that. My code does not work. I must call the row numbers from this RowID matrix since I have large data set.
Input:
new =
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0.1 0.8 0.5 0.1 0 0.4 1 1.4 1 0.5
0 0.1 0 0 0.1 0 0 0 0 0
0 0.1 0 0 0 0 1.4 0 0 0.2
0 0 0 0 0 0 0 0 0 0
RowID = [1 2; 3 4; 5 6; 7 8; 9 10];
for id=1:10
for ix=1:1:10
St(ix,1)=sum(new(:,id)([RowID(ix,1):RowID(ix,2)],:));
end
end
exmple answer for summation of 7 and 8 row across all columns of new matrix is
output=
0.1 0.9 0.5 0.1 0.1 0.4 1.0 1.4 1.0 0.5
the cyclist
the cyclist 2014년 6월 17일
Sorry for continuing to ask questions, but I am trying not to waste your time with an inaccurate solution.
Could you ever have something like
RowID = [1 5; 6 8]
where you are summing more than two consecutive rows?

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

답변 (1개)

the cyclist
the cyclist 2014년 6월 17일
Does this do what you want?
numberSets = size(RowID,1);
St = zeros(numberSets,size(new,2));
for ns = 1:numberSets
St(ns,:) = sum(new(RowID(ns,1):RowID(ns,2),:),1);
end

카테고리

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