How can I add the values of each iteration of a for loop, where the index is k=0:0.001:.30, into a matrix so that I can plot the values?

7 vues (au cours des 30 derniers jours)
I have a 'for' loop that is calculating an eqn from k=0:0.001:0.30, the loop is generating the required results for each single iteration but when I try to take each iteration and put it into a matrix I get the following error,
Subscript indices must either be real positive integers or logicals.
I think this is because of my indexing k=0:0.001:0.30, MatLab doesn't like the zeros, but I need to run the loop in this fashion to get the results I need.
This is what I have for the 'for' loop,
% preallocate space x= zeros(300,1);
for k = 0:0.001:0.30
x(:,k)=y % store y as kth column of x

Réponse acceptée

Youssef  Khmou
Youssef Khmou le 20 Mai 2014
Modifié(e) : Youssef Khmou le 20 Mai 2014
If H is scalar you can vectorize the problem :
Using the loop, the index must be an integer, to respect this condition you can proceed as :
for t=1:length(k)
x(:,t)=y; % store y as kth column of x
  1 commentaire
Mark le 20 Mai 2014
Thank you so much for clarifying this, 'H' is a scalar so I jsut used your first suggestion

Connectez-vous pour commenter.

Plus de réponses (0)


En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by