Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
How can i write this function in matlab? i got matrix eror
n=1:100; 
k=0.5; 
x[n]=sin((3*pi*n)/20); 
y(n)=x(n./k);
scale (x,k);
Posted
Updated 29-Dec-13 11:07am
v2
Comments
Er. Tushar Srivastava 29-Dec-13 14:47pm    
I guess you need to loop while using n in x(n) and y(n)
Member 10493429 29-Dec-13 14:57pm    
i defined in this way
>> n=1:100;
>> k=0.5;
>> x(n)=sin((3*pi*n)/20);
>> y(n)=x(n/k);
Index exceeds matrix dimensions.
Er. Tushar Srivastava 29-Dec-13 15:05pm    
that is obvious. Matrix Index start from 1 and if you will divide 1 by 0.5 = 0.5. That's why Matrix Index exceeds. Are you are trying to do x(n)/k ?
Member 10493429 29-Dec-13 15:18pm    
no iwant to do x(n/k), itried to devide 0.5 to 0.5. but still have matrix eror .
Er. Tushar Srivastava 29-Dec-13 15:25pm    
I said that already. index of a vector in MatLab starts with 1 i.e. x(1), x(2), x(3) ... so on. So you cannot have x(0) or x(0.5) etc. This is the reason of your error. when you do x(n/k), the first value of n = 1 so, it becomes x(1/0.5) = x(0.5) which exceeds the range i.e x(n) where n is a natural number excluding 0 i.e. from 1 to infinity. I hope it explained the error :)

1 solution

C#
1  n=1:100;
2  k=0.5;
3  x[n]=sin((3*pi*n)/20);
4  y(n)=x(n./k);
5  scale (x,k);


I put line numbers for reference...
Line one is fine, creates a vector from 1 to 100.
Line two defines a scalar value k.
Line three, this is invalid, you can't use a vector as an index. You can either loop or simply remove the index reference.
x=sin((3*pi*n)/20); %valid

Line four, same thing... invalid because you're using a vector as an index. You also don't need the ./ operator when one is a vector and the other is a scalar, it doesn't make sense. Also, this n/k will give you non-integer values for some values of n, which is not a valid index.
 
Share this answer
 
v3
Comments
Er. Tushar Srivastava 29-Dec-13 17:44pm    
Actually if line 3 is written as x(n) instead of x[n], the result will be a vector. Considering line 3 in question as x[n] being a typo error and assuming it as x(n). It is also valid. The main problem is with line 4 i.e. y(n) = x(n./k) or x(n/k); Here, n/k will result in a non integer and so the error (that's what you said is correct).
Albert Holguin 29-Dec-13 18:09pm    
If you write it as I did the result is also a vector. Matlab can determine that on its own.
Er. Tushar Srivastava 29-Dec-13 18:14pm    
I wrote line 3 as you have written and it throw an error (Unbalanced Something....)
Er. Tushar Srivastava 29-Dec-13 18:16pm    
Ok I got it, sorry I didn't look at the next corrected step. My eyes scrolled too fast off the codes :-P
Albert Holguin 29-Dec-13 18:24pm    
I work with Matlab every day... :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900