Printing arrays based on index

7 visualizaciones (últimos 30 días)
Kyle
Kyle el 6 de Jun. de 2012
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.

Respuesta aceptada

Image Analyst
Image Analyst el 6 de Jun. de 2012
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);
  1 comentario
Kyle
Kyle el 6 de Jun. de 2012
Ah, I see it know. Thanks!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by