Clear Filters
Clear Filters

How can i define this in matlab

2 views (last 30 days)
Folakemi Omotoye
Folakemi Omotoye on 6 Aug 2018
Commented: Dennis on 7 Aug 2018
I want to define this function {s(-1)} in matlab. it is giving me error message. array index must be positive integers or logical value. i have search through mathworks answers on how to rewrite this but i did not get an answer peculiar to this kind of situation.
  7 Comments
Jan
Jan on 6 Aug 2018
@Folakemi Omotoye: This is not Matlab code. Therefore we cannot know, what it is intended to do. "s(-1)=j(-1)==0" is not meaningful in Matlab. "for t=0" is not a loop, but the same as "t=0". "s(t)=sum(t-1)+sum(t)" is not meaningful, because sum is not defined here and because t is 0, you cannot use it as index. "[]=t(j>10)" is not clear also.
Please explain, what you want to achieve. Posting pseudo-code in an unknown notation does not clarify anything.
Folakemi Omotoye
Folakemi Omotoye on 7 Aug 2018
this is the whole algorithm
m=zeros(10,10,30); %define 3-D 10 by 10 by 30 matrix
n=30; %number of matrix
for k=1:n
T=linspace(0,1.0,n); %specify the value an element will take in each iteration
m(3,3,k) = T(k); %the 3rd row and 3rd column in each matrix will have the value of t
% disp(m(:,:,k));
vector=sum(sum(m)); %sum all the colums of the vector into a row
end
[v]=vector(:,:); %convert the vector into 1D vector
t=logical(T);
%CUSUM Algorithm
h=4; %threshold
s(-1) = 0;
g(-1)=0 ; %initial conditions
for t=0 %start time
s(t)=sum(v(t-1) + sum(v(t))); %cumulative sum up to current value
g(t)=max(g(t-1) + s(t)); %current value minus minimum value in vector up to current value
if g(t) > h %if output is greater than the specified threshold
detect_tim=t; %store the detection time
disp(['The detection time: (', num2str(detect_tim),')']); %display the detection time
change_time=(t(min(s(t))) + 1); %estimate the change time
disp(['The change time estimate: (', num2str(change_time),')']) %display value of change
% stop and reset algorithm
return %to re-start algorithm
end
t=t+1;
xL = get(gca,'XLim'); %plot threshold line on same axis
figure(1), subplot(2,1,1)
line(xL,[0.6 0.6],'Color','r'); %horizontal line through the threshold value
hold on
plot(1:t,v(1:t),'*b') %the time vector plot
title('signal change plot')
xlabel('time (ms)')
ylabel('Amplitude (v)')
end

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 6 Aug 2018
  18 Comments
Folakemi Omotoye
Folakemi Omotoye on 7 Aug 2018
t=logical(T) %to convert T of class double to logical
s(t)=sum(v(t-1) + sum(v(t))); %if (t-1) was execuatble, it would have been the current sum plus cumulative sum of last iteration
g(t)=max(g(t) + s(t)); %same explanation as above.the max value of past iterations of g(t) till current iteration
if g(t) > h
detect_tim=t; %it will only write when g(t) is greater than h, else it restarts iteration from beginning
disp(['The detection time: (', num2str(detect_tim),')'])
change_time=(t(min(s(t))) + 1); %it should be the time following the time at which g(t) was greater than h
disp(['The change time estimate: (', num2str(change_time),')'])
return % it should re-start the algorithm from the beginning rather than continuing with the next iteration
end
t=t+1; %noted
Dennis
Dennis on 7 Aug 2018
s(t)=sum(v(t-1) + sum(v(t))); %if (t-1) was execuatble, it would have been the current sum plus cumulative sum of last iteration
No.
If v=[1 2 3 4] was your vector s(4) would be sum(v(3)) + sum(v(4)) which is the same as v(3) + v(4) -> 7
The cummulative sum would be v(1)+v(2)+v(3)+v(4) =1+2+3+4=10, but thats no what your code is calculating.
This works if you write
s(t)=s(t)-1+v(t) %s(t-1) !!!!!
And again sum(v(t)) is the same as v(t).

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!