Attempting to take the sum of an index in my loop
9 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hello everyone,
In my script, I am calculating RMSDs of a variety of things, and creating a 155x2 matrix.
predictions=load('Sparta_Predictions2.txt');
experimental=load('ExperimentalValue_Unk2.txt');
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 Commenti
Risposte (1)
Vedere anche
Categorie
Scopri di più su Operating on Diagonal Matrices in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!