Printing arrays based on index

Im having trouble figuring out why my print statement wont print the first index of several different arrays on one line, the second indexes on the next, and so on. I'm getting at error: Index exceeds matrix dimensions.
% Define variables:
% nw- Null frequency
% x- computed value
% y- formula value
% z- percent error
% length- number of coefficients
% c,k- vectors to get positive nulls
% Code starts here:
%Get length
length=input('Please input the number of coefficients: ');
%Compute phase, magnitude and plot
bb=ones(1,length);
[HH,ww] = freqz(bb,1,-pi:(pi/100):pi);
subplot(2,1,1);
plot(ww,abs(HH))
title('Figure 1')
subplot(2,1,2)
plot(ww,angle(HH))
title('Figure 2')
%Compute formula value
k=1:length/2;
y=2*pi*k/length;
%Computed value
c=1:length/2;
nw= find(abs(HH)<.01);
freqvect=(-pi:(pi/100):pi);
x=freqvect(nw(c+length/2));
%Print and compute %error
for i=1:length/2
z=((y(i)-x(i))/y(i))*100;
fprintf('Null frequency #%d, computed value is %d, formula value is %d, error is %d \n',i,x(i),y(i),z(i));
end
I'm working with length=4. I get 4 nulls but only want the positives so i just manipulated x to get the positives. This is the best result I've been able to come up. I get the first line but then i get the error. I realize that if the length is odd i may run into problems but it indexes by 1 every time so i think it would just ignore it.

 채택된 답변

Image Analyst
Image Analyst 2012년 6월 6일

0 개 추천

z is not an array, so there is no z(i). Okay, maybe there is a z(1) since z is a scalar, but there definitely is no z(2). Either assign z(i):
z(i) = ((y(i)-x(i))/y(i))*100;
or print z, not z(i):
fprintf('Null frequency #%d, computed value is %d, formula value is %d, error is %d \n',i,x(i),y(i),z);

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

질문:

2012년 6월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by