Variable indexing for loop when vector is negative (Array indices must be positive integers or logical values)

2 ビュー (過去 30 日間)
Ava
Ava 2020 年 10 月 3 日
回答済み: madhan ravi 2020 年 10 月 3 日
Need to do a for loop when the vector is negative
x = -10:1:-1
n = 1:1:10
Trying to find
a(x) = 12 * x(n)^5
Instructions say to use a variable index to find a(x) because otherwise I get this error "Array indices must be positive integers or logical values."
How do I use a variable index for this?

回答 (2 件)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2020 年 10 月 3 日
Hi,
Vectorization is the most efficient way, e.g.:
a = 12*x.^5;
Or if you need to do via a loop, then
for ii=1:numel(x)
a(ii) = ... % Figure out how to write the rest
end

madhan ravi
madhan ravi 2020 年 10 月 3 日
a = @(x) 12 * x.^5;
a(x)

カテゴリ

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