Help with for loop for a function

6 ビュー (過去 30 日間)
Andrea
Andrea 2014 年 11 月 23 日
回答済み: Andrea 2014 年 11 月 23 日
I am trying to run the following for loop for different values of t from 0.01 to about 10 however I keep on getting Attempted to access M(0.1); index must be a positive integer or logical. Error in untitled3 (line 3)
When i change t to be a integer the for loop runs perfectly fine and saves the output into a vector M. I also tried using the cell command but it didn't work. Any help would be appreciated. The function does take any number, it doesn't have to be an integer. When I run the function for specific values it does work.
M=[];
for t=0.01:0.01:10;
M(t)=dtmf_attack(t);
end
display(M)

回答 (3 件)

Orion
Orion 2014 年 11 月 23 日
編集済み: Orion 2014 年 11 月 23 日
just add a counter
M=[];
i=0;
for t=0.01:0.01:10;
i=i+1;
M(i)=dtmf_attack(t);
end
display(M)

Image Analyst
Image Analyst 2014 年 11 月 23 日
編集済み: Image Analyst 2014 年 11 月 23 日
Try this:
% Define t
t=0.01:0.01:10;
% Build M:
M = zeros(1, length(t)); % Optional preallocation.
for k = 1 : length(t)
M(k) = dtmf_attack(t(k));
end
% Display M
M

Andrea
Andrea 2014 年 11 月 23 日
Thanks they both worked

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by