How to index for saving an output of a for loop for each loop?

3 vues (au cours des 30 derniers jours)
Wolfgang McCormack
Wolfgang McCormack le 13 Mar 2021
Commenté : Sergey Kasyanov le 13 Mar 2021
Hi all, I have the following code and I want to save the output but how?
for i = 0.1:0.1:0.7
'Calculating somestuff here'
'S = output of modifications which is a 4 x 1 array'
'Now I want to wrtie the output to an array but I cannot use i because it's fractional'
Saved(:,i) = s 'but it does not work'
end
I tried this but no help:
for z = 1:100
for i = 0.1:0.1:0.7
'Calculating somestuff here'
'S = output of modifications which is a 4 x 1 array'
'Now I want to wrtie the output to an array but I cannot use i because it's fractional'
Saved(:,z) = s 'but it does not work'
end
end
I will definitely appreciate your help!

Réponse acceptée

Sergey Kasyanov
Sergey Kasyanov le 13 Mar 2021
Modifié(e) : Sergey Kasyanov le 13 Mar 2021
Hello,
try that
I = 0.1:0.1:0.7;
Saved = zeros(4,length(I));
for i = 1:length(I)
'Calculating somestuff here'
'Use I(i) instead i'
Saved(:,i) = S;
end
  2 commentaires
Wolfgang McCormack
Wolfgang McCormack le 13 Mar 2021
@Sergey Kasyanov thank you Sergey! Worked like a charm! Just two quick question. So, as it is checking an if statement and then saves, when the first three logical index do not match the if, 0s are shown until it reached a column where the if statement matches the output. How can I ignore 0s in the saved file and have only the actual values?
Second, I am saving an output which is 4x1. How can I save it vertically(row wise but in each 4 row can only be one output as the output is 4x1) instead of columns wise?
Thank you so much!
Sergey Kasyanov
Sergey Kasyanov le 13 Mar 2021
All problems can be solved in slow but working way
I = 0.1:0.1:0.7;
Saved = [];
for i = 1:length(I)
'Calculating somestuff here'
'Use I(i) instead i'
Saved = [Saved; S'];
end

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by