A problem with a matrix
Show older comments
Hello. I am doing research connected with magnetic observatory data. I have 50 observatories and each of them produces 12 directories a year (for every month) with 30 files inside. That is why I need a loop. I downloaded data from the files successfully and counted a "MA-value" in a Class.
for k=1:1:12
meas=CCmpNew(1000,rect,0); %MA-value in a class
a = find(meas<0.4);
for i=1:kmax %kmax is the number of days in a month
b = a>(i-1)*1440 & a<=i*1440;
n(i)=sum(b)/1440;
for j=1:kmax*1440
if j>(i-1)*1440 && j<=i*1440
if meas(j)>0.55
n(i)=0;
end
end
end
end
[N,I]=sort(n,'descend');
for d=1:nnz(N) % It seems to me that the problem is in this loop.
kdu_I(d)=I(d);
kdu_N(d)=N(d);
K_(d)=k;
end
C=[K_' kdu_I' kdu_n'];
dlmwrite('path',C,'-append');
end
The problem is that when I write C matrix (actually, append it to file), if on some iteration there were less elements in C than on the previous iteration, the programme completes C up to the dimension of it on the previous iteration. How can I fix it? Could you please help me with this? I know my mistake is a silly one, but I'm beginner...
2 Comments
To make it easier for others (and yourself) to follow your code, I would recommend that you use
- proper indenting for loops, ifs, etc.
- meaningful variable names, so that we don't have to go back through the whole code to remember what was a, b, c, d, n (and N!), etc. If k is the directory index, why not call the variable directoryindex. Immediately, it's clear on any line where it's used, what it contains.
- comments that explain what the purpose of each loop is.
John D'Errico
on 17 Nov 2016
So many times when I want to be able to upvote a comment. Guillaume's comment is spot on. The code posted is quite difficult for anyone to read. And in order for us to debug that code, we need to completely understand what the OP is doing, and why. Since all we have to go on is the code, that can be nearly impossible to do.
Writing code that you can actually read is incredibly valuable as a programmer. It allows you to return to that code next month or next year when possibly you find a problem.
Accepted Answer
More Answers (0)
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!