how to solve this error

3 ビュー (過去 30 日間)
brito antony
brito antony 2023 年 10 月 28 日
回答済み: Arun 2023 年 11 月 1 日
Array indices must be positive integers or logical values.
clear all
clc
n = [-5:5];
h = [1 0 2 0 3 0 0 0 0 0 0];
y = 3 * h(-n-3);
stem(n,y)
  1 件のコメント
Dyuman Joshi
Dyuman Joshi 2023 年 10 月 28 日
The error is clear - Indices in MATLAB can not be negative or non-integeral values.
They must be postivie integers or logical values (as the error states).
You are using (-n-3) as indices which has negative values.
Also, h is mapped for [-5 5], whereas (-n-3) ranges from [-8 2]. What should be the values when n is -8, -7, or -6?

サインインしてコメントする。

回答 (1 件)

Arun
Arun 2023 年 11 月 1 日
Hey Brito,
The variable 'h' has index values ranging from 1 to 11. To obtain the desired results, you may consider modifying the code as shown in the following snippet.
n = [-5:5];
h = [1 0 2 0 3 0 0 0 0 0 0];
y = 3 * h(-(-n-3-3)); % or y = 3 * h(n+6);
y
y = 1×11
3 0 6 0 9 0 0 0 0 0 0
stem(n,y)
Hope this helps.

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by