Hello
Excuse me, how can I enter the following function in matlab?
y[n]=x[n]+x[n-1]
I have the vector [1 0.5 0.75 0.5 1] that corresponds to each value of n evaluated.
n=0:5
My code is
clc
clear all
vec=[1 0.5 0.75 0.5 1 0];
for i=1:6
n2= i-1;
ynum= vec(i)+ vec(n2);
y= ynum./2;
hold on
stem (i,y)
end
and the error that matlab tells me is
Array indices must be positive integers or logical values.
Error in Memoriafunejemplo (line 8)
ynum= vec(i)+ vec(n2);
Thank you very much for your comments!

2 件のコメント

Jeffrey Clark
Jeffrey Clark 2022 年 11 月 7 日
@Mechelle Zarahi Fuentes Perez, MATLAB indexs are 1 based so vec(n2) is invalid since n2 is zero the first time through your loop. You must decide how y(1) is to be defined.
Mechelle Zarahi Fuentes Perez
Mechelle Zarahi Fuentes Perez 2022 年 11 月 25 日
Thank you very much, that was my mistake!

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

回答 (1 件)

Walter Roberson
Walter Roberson 2022 年 11 月 7 日

1 投票

vec=[1 0.5 0.75 0.5 1 0]
vec = 1×6
1.0000 0.5000 0.7500 0.5000 1.0000 0
vm = movmean(vec, 2)
vm = 1×6
1.0000 0.7500 0.6250 0.6250 0.7500 0.5000
plot([vec; vm].')

カテゴリ

Community Treasure Hunt

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

Start Hunting!

Translated by