Not able to figure out the way to deal with large vectors

조회 수: 1 (최근 30일)
Bhaskar
Bhaskar 2023년 7월 20일
편집: Harsh Kumar 2023년 7월 20일
Hi ,
I was doing problem and faced a issue how to deal with large vectors .
I have a function
x(n) = e0.1n [u (n + 5) – u (n − 10)]
and want to fetch the even and odd components of it to perform some operations over them .The issue i am computing them by iterating through the since to get access of the values and checking if it is odd or even and perform operation right away there.But it might be too silly to use when 'n' value goes very high .
can anyone suggest any good way for that ? Here is the Pseduo code
n=-10:1:10;
for loop
%%cheking the conditions if the current x(n)
%%value is odd or even and performing operations there only .
end
%%plot the parts

채택된 답변

Harsh Kumar
Harsh Kumar 2023년 7월 20일
편집: Harsh Kumar 2023년 7월 20일
Hi Bhaskar ,
I understand that you are looking to compute the odd and even parts of a function optimistically in MATLAB .
To achieve this, you can use the vector definition of function and use the theoretical definition of odd/even function to compute them optimistically .
Odd= (func(x)-func(-x))/2
Even= (func(x)+func(-x))/2
Refer to the below code snippet for better understanding .
n=-50:1:50;
func1=exp(0.1*n).*((n>=-5)-(n>=10)); % orginal function
func2=exp(-1*0.1*n).*((n<=5)-(n<=-10)); %conjugate function
odd=(func1-func2)/2; %odd function calculated using given formula
even=(func1+func2)/2; %even function calculated using given formula
stem(n,odd);
figure();
stem(n,even);
Refer to the documentation for more details on vector operations :https://in.mathworks.com/help/stateflow/ug/operations-for-vectors-and-matrices.html

추가 답변 (2개)

KSSV
KSSV 2023년 7월 20일
n=-10:1:10;
c1 = 0 ; c2 = 0 ;
E = zeros([],1) ;
O = zeros([],1) ;
for i = 1:length(n)
%% Get your x(n)
if mod(x(n))
c1 = c1+1 ;
% Odd do your calculations
% save the value
O(c1)=val ;
else
c2 = c2+1 ;
% even do your calculation
% save the value
E(c2) = val ;
end
end
%%plot the parts
plot(E,'r')
hold on
plot(O,b)
Alternatively, you can calculate x in the loop and later find it is even or odd using mod. This would be vectorised.

Walter Roberson
Walter Roberson 2023년 7월 20일
mask = mod(x,2) == 1;
Now do the "odd" operations on x(mask) and do the "even" operations on x(~mask).

카테고리

Help CenterFile Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by