- I don't see an actual question and it's really not clear what it is you want to achieve
- Nothing in your loop depends on k. What is the purpose of the loop?
- Time and time again we say on this forum that numbered variables are a very bad design. Why do you have 10 different variables DxxAwdiff instead of a single matrix or cell array?
Saving Data for all files to matrix
5 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I have seen this question loads of times and have also tried different answers but I cannot seem to find the correct one. The following code is in a loop:
for k = 1:length(FNM)
AvgAwdiff = abs(D1Awdiff + D2Awdiff + D3Awdiff + D4Awdiff + D5Awdiff + D6Awdiff + ...
D7Awdiff + D8Awdiff + D9Awdiff + D10Awdiff) / length(TallyCheck);
AvgAwdiff= round(AvgAwdiff);
AvgAwdiff = num2str(AvgAwdiff)
end
I have tried AvgAwdiff(k,:) and AvgAwdiff(k) or AvgAwdiff{k}. But these either give me an error or a character that does not have the correct values. I followed the data in the command window and everything works fine but it is just the save the values in a matrix that I can't seem to do.
18 Commenti
Guillaume
il 31 Mag 2018
Yes, this is a lot clearer. A few questions:
- Do the DxAWdiff and TallyCheck vary per FNM (something you haven't shown so far) or are they constant throughout?
- Is your problem that you want to sum a different number of DxAWdiff variable per FNM (and you don't know how to do that?
- How are the DxAWdiff created?
Risposta accettata
Stephen23
il 31 Mag 2018
Modificato: Stephen23
il 31 Mag 2018
You need to reference the output variable using indexing, otherwise you simply overwrite it on each loop iteration. Assuming that on each iteration you calculate exactly one value, then try something like this:
AvgAwdiff = nan(size(FNM)); % preallocate!
for k = 1:numel(FNM)
... whatever calculations that depend on k.
tmp = D1Awdiff + D2Awdiff + D3Awdiff + D4Awdiff + D5Awdiff + D6Awdiff + D7Awdiff + D8Awdiff + D9Awdiff + D10Awdiff;
tmp = abs(tmp) / length(TallyCheck);
AvgAwdiff(k) = round(tmp);
end
See also:
PS: Numbered variables are generally a sign that you should be using arrays and indexing. For example, rather than D1Awdiff, D2Awdiff, etc. putting the data into one matrix D1wdiff would mean you could simply do this:
sum(D1wdiff)
0 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Loops and Conditional Statements 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!