# Attempting to take the sum of an index in my loop

조회 수: 7 (최근 30일)
Sam Mahdi 2019년 10월 2일
답변: Sam Mahdi 2019년 10월 2일
Hello everyone,
In my script, I am calculating RMSDs of a variety of things, and creating a 155x2 matrix.
x=predictions(:,1);
error=predictions(:,2);
y=experimental(:,1);
z = zeros(1,6);
sizevaly = length(y)/6;
sizevalx=length(x(:,1))/6;
b = zeros(sizevaly,sizevalx);
d=(1:sizevalx);
% e=zeros(sizevaly,6);
e=zeros(1,6);
for n=1:sizevalx
for j=1:sizevaly
for i=1:6
xindex = i+(6*(n-1));
yindex=i+(6*(j-1));
z(i)=((x(xindex)-y(yindex)))^2;
e(i)=(z(i)/(error(xindex)^2));
if e(i)>1000
e(i)=0;
end
b(j,n)=sqrt((1/5)*sum(e,2));
end
end
A=b';
end
I also have a diagonal vector, that is filtering my 155x2 matrix by setting the values to 0 and 1 according to a certain threshold.
[m,n] = size(A);
pattern = false(m,n);
dvec = 1 : m+1 : 1 + (n-1)*(m+1);
for ni = 1 : m-n+1
pattern(dvec) = all(A(dvec)<2);
dvec = dvec + 1;
end
What I also wanted to do, in addition to the above filtering, was take the sum of that diagonal vector. To this end I have tried to write a simple loop similar to the above:
a=zeros(m)
for nt=1:m-n+1
a(nt)=sum(A(dvec))
dvec=dvec+1;
end
In short, I have made a vector of zeros the same size as my data. Then I want to take the sum of the daigonal vector and store it in my vector a.
The issue is I get this error:
Index exceeds the number of array elements (310).
Error in For_Sparta (line 45)
a(nt)=sum(A(dvec))
From what I've looked up, it appears to mean that I'm not actually storing the sum values from each iteration. However from my understanding, all you have to do is index your loop values and that should store them for every iteration (which is what I have attempted to do with a(nt)). So a(1) is the sum of my daigonal vector in my data A, and then dvec is added by one, and then a(2) is the sum of my diagonal vector+1.
I.E. In other words:
If we say I have a matrix
G =
1 1
1 2
1 3
Then I want to make a vector with the sum of the diagonal in this. So say vector a would be
a =
3
4
In this example.
##### 댓글 수: 6이전 댓글 4개 표시이전 댓글 4개 숨기기
darova 2019년 10월 2일
Is this correct diagonal?
Sam Mahdi 2019년 10월 2일
Yes. On a numberical level here is what it should look like:
>> A
A =
0.5621 1.7063
1.4955 2.3217
1.3758 3.2607
>> sum(A(dvec))
ans =
2.8839
So if you run the sample set I provided, you will get those values of A. If you just use dvec at its initial value of 1 and 5, that will give you the above answer. The thing is, I want to take this basic concept and make it into a loop for larger data sets. So the loop I have now should do dvec+1, so that'll give 2 and 6.
sum(A(dvec+1))
ans =
4.7562
And so on and so forth. I see that one of my issues is the final iteration where dvec is 3 and 7 will cause an error, since there is no 7th value. However from my understanding, setting my iterations to 1:m-n+1 should limit the number of diagonal vectors (so in this case it would only be 1:2 since you can only get 2 diagonals since there is no 7th value.

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

### 답변 (1개)

Sam Mahdi 2019년 10월 2일
I guess I'll post this as an answer. The issue appears to be my diagonal vector had to be redfined. It was using the diagonal vector from my previous loop it seems, and that would cause an error since the last iteration of that loop will actually have values missing in the diagonals, hence causing the error. All I had to do was redfine my diagonal vector.
a=zeros(m);
dvec = 1 : m+1 : 1 + (n-1)*(m+1);
for nt=1:m-n+1
a(nt)=sum(A(dvec));
dvec=dvec+1;
end

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

### 카테고리

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

### Community Treasure Hunt

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

Start Hunting!

Translated by