Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to use negative indexing in Matlab?

Status
Not open for further replies.

sayurabh

Member level 4
Joined
Apr 11, 2007
Messages
69
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,783
why matla doesnot accept the negative indexing the array u understand what i ms aying for e.g
>> x=1:4;
>> n=-2:2;
>> x(n)
??? Subscript indices must either be real positive integers or logicals.
that is the problem why we cannot do negative indexing i cannot do sigshifting and adding both and i cannot calculate even and odd parts of the discrete signal plz solve my problem
 

sigshift + matlab

When you type x(n), what answer are you hoping to get?
 

matlab problem

why should you need to use negative index?
In your example, the index of X is from 1 to 4,
not from -2 to 2.

Ryan
 

Re: matlab problem

n n=-2:2
and x=1:4;
x(n) means
x(-2)=1
x(-1)=2
x(0)=3;
in this way but i want solution for negative indexing du to this i cannot sort even and odd parts of a dicret signal it requires fliplr command when reversed and added it give error of negative indexez
 

matlab problem

Why not use
x(-2+2)=1
X(-1+2)=2
X(0+2)=3

This way can also achieve your goals.
Maybe you should map negative number into positive index.

Ryan
 

matlab problem

Hi sayurabh, Your clarification leaves me wondering what x(2) will be. ;)

As RDRyan suggested, in most programming languages it's common to simply add a constant to the array indexes to move them into allowed range.
 

Re: matlab problem

i want to do convolution of the 2 signals while creating matlab code i have to do signal folding due to this negative idexing occurs on multiplication it gives error i pasting whole convolution code of 2 signals
function [y,j]=convu(x1,n1,x2,n2)
sum=0;
for j=min(n1,n2):max(n1,n2)
[x4,n4]=sigfold(x2,n2);
[y1,n3]=sigshift(x4,n4,j);
for k=n1
p=x1(k).*y1(k);
sum=sum+p;
end;
y(j)=sum;
end;
i m not able to fing solution for negaive indexing in this code to do convoltuion x1*x2 with length n1=x:y;
n2=f:u;
sigfold means to reverse the whole signal and sigshift means shifting the origin to a point j it also shifts the whle signal
 

matlab problem

Your example has several undefined things. Can you show a complete example including input values?
 

Re: matlab problem

tell me waht seems u undefined i will post code for subfuctions of the code like sigshift and sigfold if u like tell me what is undefined i want this code to work at any cost i will give u the code for sigashift and sigfold

Added after 32 minutes:

function [y,n]=sigfold(x,n)
y=fliplr(x);
n=-fliplr(n);
code for signal fiolding
function [y,n]=sigshift(x,m,no)
n=m+no;
y=x;
stem(n,y);
code for signal shifting u can paste them in command window in matlb u can check it they r working absolutely fine save it both as.m filae than my convolution code as convu.m and then pass the parameere nothing is undefined we have to do x1*x2 with x1 icontains n element with n1 indexing same it is x2 we are doing x1*x2 for e.g check it now my ans is nt coming i have this code myself but my answer is not coming according the book
x1(n)=[3 11 7 0 -1 4 2],n1=-3:3,x2(n2)=[2 3 0 -5 2 1] , n2=-1:4
x1*x2 should cocme out to bey(n)=[6 31 47 6 -51 -5 41 18 -22 -3 8 2] it is correct theoretically is true correct it plz it has it my whole blood
 

matlab problem

Ok, everything is defined now. However your example input values n1 and n2 have unequal dimensions, so convu immediately fails at min(n1,n2) and max(n1,n2). I don't know what you are trying to compute for j.

From MATLAB 7.3 help:
"C = min(A,B) returns an array the same size as A and B with the smallest elements taken from A or B. The dimensions of A and B must match, or they may be scalar."

Why don't you simply use MATLAB's conv function? Maybe this is a learning project?

You may be making the project more difficult than it needs to be. MATLAB's conv function doesn't input or output arrays of index values. Do you really need to do it?

How about this? (paste into Command Window)
Code:
x1 = [3 11 7 0 -1 4 2];
x2 = [2 3 0 -5 2 1];
x1z = [zeros(1,length(x2)-1) x1 zeros(1,length(x2)-1)];
for i = 1 : length(x1) + length(x2) - 1;
  y(i) = sum(x1z(i : i+length(x2)-1) .* fliplr(x2));
end
y
y =
6 31 47 6 -51 -5 41 18 -22 -3 8 2
 

Re: matlab problem

using the predefined functions is jnot the learning and making u expert in any line use ur own mind i try to use minimum predefined function so in this prob i had solved yr query see in the ans u r saying right the for loop main one should go from min(n1)+min(n2):max(n1)+max(n2) according to answer ayyary indexing is u can check it one error is removed thanks for it to fing my errors but code is right na according to theoritical convolution
 

matlab problem

It's good to be theoretically correct, but you must also satisfy MATLAB's rules.
Offset all your array index values so they begin at 1.
Don't try to read non-existent array elements.
Do you need help modifying your code?
 

Re: matlab problem

u r not understanding main problrm iz coming in negative indexing matlab cannot find the meaning of x(-3) what should i do tell me solution i want to avoid negative indexing but conv code cannot be made without -tive indexing the in built conv do the convolution taking the first elements of the both signals as a 0 indexing u uderstand that is first element would start from 0 index
 

matlab problem

you will surely be convoluting a finite number of samples of each signal. We all know that the formula will give you negative values, but the idea is to slide the reversed h(n) over your input x(n)... then the results will be in temporal order.

If you need the exact time of your output samlpe, then just be really careful in seing it.

The easiest way is to have a causal system, like everyone is telling you, just shift everything to start from 0. The result is the same, just shifted in time...
 

Re: matlab problem

cauaual system is the system that depend upon on the past and present values of the dependent variables i know the definituion of convolution fuction but tell what u r saying plz expalin indetail i cannot understand u can giv ee.g for best demonstration or u plz correct m convolution code
 

matlab problem

Try this:
Code:
x1 = [3 11 7 0 -1 4 2];
n1 = -3 : 3;
x2 = [2 3 0 -5 2 1];
n2 = -1 : 4;
m1 = length(x1);
m2 = length(x2);
clear y n
for j = 1 : m1 + m2 - 1;
  sum = 0;
  for k = 1 : m2
    if ((j + k - m2) >= 1) & ((j + k - m2) <= m1)
      sum = sum + x1(j + k - m2) .* x2(m2 - k + 1);
    end
  end
  y(j) = sum;
  n(j) = n1(1) + n2(1) + j - 1;
end
stem(n,y);
display(y);
display(n);
y =
6 31 47 6 -51 -5 41 18 -22 -3 8 2
n =
-4 -3 -2 -1 0 1 2 3 4 5 6 7
 

    sayurabh

    Points: 2
    Helpful Answer Positive Rating
Re: matlab problem

oh man great!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!it is working wow i spemnd a lot of hrs on i to make this correct thank s lot
 

Re: matlab problem

function [v3,n1]=convumine(x1,n1,x2,n2)
m2=length(x2);
m1=length(x1);
b1=m1+m2-1;
if(m1>m2)
l1=m2;
while(m1>l1)
v2=[x2,zeros(1,1)];
l1=length(v2);
end;

y2=v2;
else y2=x2;
end;
for a9=1:b1
sum=0;k=0;
if(a9<=m1)
for k1=1:a9
sum=sum+x1(k1)*y2(a9+1-k1);
end;
end;
if(a9>m1)
if(m1<m2)
l4=m1;
while(m2>l4)
v6=[zeros(1,1),x1];
l4=length(v6);
end;
x1=v6;
end;
for d1=a9-(b1-m1):m1
sum=sum+x1(d1).*x2(m2-k);
k=k+1;
end;
end;
v3(a9)=sum;
end;
n1=min(n1)+min(n2):max(n1)+max(n2)
my own matlab code for convlution gotcha i develop it by myself no buk help no copy u an see that it is original urs code was alos great but mine is mine i want to develop code on my strength i have done it where is will there is a way u can chec out which code id more powerful
 

Re: matlab problem

Hi sayurabh, Your question is difficult to read -- several strange words and no punctuation.

Your new code gives the correct answer with your sample input data. However, if I add another data point to x1,n1, then the code loops forever. I didn't try to debug it, because the code is difficult to understand. For clarity and faster performance, I suggest using MATLAB's array math instead of loops and variables.

My second example also used too many loops and variables. I wrote it that way to demonstrate a calculation using only positive index values.

Here's my first example, modified to include "n" inputs and output.
Code:
x1 = [3 11 7 0 -1 4 2];
n1 = -3 : 3;
x2 = [2 3 0 -5 2 1];
n2 = -1 : 4;
m1 = length(x1);
m2 = length(x2);
clear y n
x1z = [zeros(1,m2-1) x1 zeros(1,m2-1)];
for i = 1 : m1 + m2 - 1;
  y(i) = sum(x1z(i : i+m2-1) .* fliplr(x2));
  n(i) = n1(1) + n2(1) + i - 1;
end
stem(n,y);
display(y);
display(n);
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top