How can i define this in matlab
3 次查看(过去 30 天)
显示 更早的评论
Folakemi Omotoye
2018-8-6
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 个评论
Folakemi Omotoye
2018-8-6
how i can re-write the following code s(-1)=0
such that i wont get the error message ''array index must be positive integers or logical value''
Folakemi Omotoye
2018-8-6
编辑:Image Analyst
2018-8-6
This is the code:
{s(-1)=j(-1)==0
t=0
for t=0
s(t)=sum(t-1)+sum(t)
j(t)=g(t-1)+s(t)
if j(t)>10
[]=t(j>10)
end
t=t+1
end
Jan
2018-8-6
@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
2018-8-7
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
回答(1 个)
Image Analyst
2018-8-6
18 个评论
Folakemi Omotoye
2018-8-6
I did convert to a logical but i a still getting an error for the line {s(t-1)=0}. this is the new code
{t=logical(T);
t=0;
h=4; %threshold
s(t-1) = 0;
g(t-1)=0 ; %initial conditions
for t=0
s(t)=sum(t-1)+sum(t)
j(t)=g(t-1)+s(t)
if j(t)>10
[]=t(j>10)
end
t=t+1
end }
p.s T is of class double, t is of class logical
Image Analyst
2018-8-6
That's because that's not valid MATLAB syntax. First of all T is not defined. Next, you can't have a line start with a brace. And you can't have a chunk of code end with a brace, like you have after the end in the final line. Braces are for doing something with a cell array. See the FAQ on cell arrays.
Next, if you do for t=0, you're only doing one iteration with a loop iterator value of zero. Essentially you're not doing a loop at all.
Next, you can't say []=t(j>10) because that is setting null to all elements of t, where the corresponding value of the j array is more than 10. You can't do that. Null is null and you can't set something equal to it. It's like if you said 300 = g. No, 300 is 300 and you can't change it. Maybe you meant t(j>10) = [], which would delete all elements of t where the corresponding value of j was more than 10, basically shortening t.
And saying sum(t) is just zero, because t is zero, and summing one element with a value of zero is just simply zero.
You really need to read the getting started section of the help because the code is full of syntax errors.
Folakemi Omotoye
2018-8-6
the braces is not part of the original code. i added it to seprate my comment from the code. t is supposed to increase by 1, then start the iteration again. sum(t) is only zero for the first iteration.subsequent iterations will have a value. i assigned a variable name to t(j>10), yet still have the error message.
one thing i observed was that, the line of converting double to logical (which i placed before the rest of the code) did evaluate but the rest of my code was evaluating t as a double.
Walter Roberson
2018-8-6
When you use a logical in a numeric context it is treated as a double in most cases.
Image Analyst
2018-8-6
I would fix it, but I honestly can't figure out what you want to do. Can you put in some comments that describe what you're trying to do? What is T? Is it an array or a single value? How many times do you want to iterate the loop? And we have no idea what you're attempting with this: "[]=t(j>10)". When you posted it you removed all the comments that I'm sure you put in there before, like any good programmer would have. Please put them back in.
Walter Roberson
2018-8-6
Are you trying to define a timesequence written in terms of the unit step function, where [-1] by convention refers to the value at the previous time step?
Folakemi Omotoye
2018-8-7
编辑:Image Analyst
2018-8-7
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(vee),')']) %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
end
Dennis
2018-8-7
Maybe you can explain what you want to achieve. This line is NOT the cumulative sum up to current value:
s(t)=sum(v(t-1) + sum(v(t)));
As Jan already stated the sum() does not do anything here.
Indexing in Matlab starts at 1, thus you can not assign any value to s(-1).
Your code looks still similar to the one you posted last week, but obviously my solution did not do what you were hoping.
Can you provide an example of input data (v=[1 3 2 4 5 1 3]) and expected output (s=[1 4 6 10 15 16 19])) g=[? ? ? ?] change_time=[? ? ? ????]).
Maybe explain what
change_time=(t(min(s(t))) + 1)
is supposed to do - I am really sure it is not working.
Folakemi Omotoye
2018-8-7
编辑:Image Analyst
2018-8-7
I earlier had my code for the sum to be
s(t)=sum(v(1:t)) + sum(v(t))
but my instructor said it should be in the form
s(t)= sum(v(t-1)) + sum(v(t))
as written in the new codes i uploaded today. it is a challenge getting it in that form because matlab wouldn't take a negative index.
v = 1x30 double, with values-
v = [ 0 0.0345 0.0690 0.1034 0.1379
0.1724 0.2069 0.2414 0.2759 0.3103
0.3448 0.3793 0.4138 0.4483 0.4828
0.5172 0.5517 0.5862 0.6207 0.6552
0.6897 0.7241 0.7586 0.7931 0.8276
0.8621 0.8966 0.9310 0.9655 1.0000]
while ''t'' is unspecified but is supposed to start from zero till the final possible iteration for ''v'' .
change_time is the time following the current minimum of the cumulative sum.
I hope my explanation is clear for you to understand.
Folakemi Omotoye
2018-8-7
the use of the cumsum command will nullify the need for the loop, which is not what the algorithm is design to use.
Dennis
2018-8-7
v(t) and v(t-1) are scalars, this means v(t) might be 3 v(t-1) might be 5. What is sum(3) supposed to be (spoiler: its 3)?
If you want to sum up the vector one by one you need to do it like this:
s=zeros(numel(v),1);
for t=1:numel(v)
if t==1
s(t)=v(t);
else
s(t)=s(t-1)+v(t);
end
end
However the resulting vector will be the same as if you used
s=zeros(numel(v),1);
for t=1:numel(v)
s(t)=sum(v(1:t));
end
or even cleaner as suggested by Image Analyst:
s=cumsum(v)
Folakemi Omotoye
2018-8-7
Thanks for the idea, i have re-written the code in this form below but the line containing
g(t)=max(g(t) + s(t));
is producing error message ''index exceeds array bounds''
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
if t==0
s=0;
g=0;
else
if t==1
s(t)=v(t);
g(t)=s(t);
else
end
end
for t=2 %start time
s(t)=sum(v(t-1) + sum(v(t))); %cumulative sum up to current value
g(t)=max(g(t) + s(t)); %maximum 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
Dennis
2018-8-7
Now its getting kinda weird. However lets go through your code (i added a comment to most lines):
m=zeros(10,10,30); %10x10x30 zeros - nothing wrong here
n=30; %probably size(m,3) ->we do not need n, but no problem
for k=1:n %k=1:size(m,3) is safer
T=linspace(0,1.0,n); %this does not change better calculate outside loop
m(3,3,k) = T(k); %see alternative below
% disp(m(:,:,k));
vector=sum(sum(m)); %this is more or less the same as linspace(0,1,30), and should not calculate this in every iteration
end
[v]=vector(:,:); %now its exactly the same as linspace(0,1,30), you do not need the [] here
I guess there are usually more non-zero elements in m, however you can replace the entire loop with
m(3,3,:)=linspace(0,1,30);
This line creates a vector of 1 zero and 29 ones:
t=logical(T); %not sure what we want with this
Now those lines don't do anything at all:
if t==0 %t is a logical vector, this will only be true when all values in t are 0 - and this is not happening
s=0; %never gets executed, is not indexed either
g=0; %never gets executed, is not indexed either
else %better use elseif in this case
if t==1 %one value in t is 0 so this is never true
s(t)=v(t); %looks okayish
g(t)=s(t); %looks okayish
else %you can omit this
end
end
This is not working:
for t=2 %this is not a loop, for t=2:numel(v)
s(t)=sum(v(t-1) + sum(v(t))); %the sum does not do anything...really and this is no cumulative sum since you only add up the last 2 values!!!
g(t)=max(g(t) + s(t)); %this throws an error because g(2) does not exist, maybe g(t)=max(g(t-1)+s(t);
if g(t) > h
detect_tim=t; %you overwrite detect_tim in every iteration
disp(['The detection time: (', num2str(detect_tim),')'])
change_time=(t(min(s(t))) + 1); %i don't think that this can work: min(s(t)) is the same as s(t) and might be a floating point number, which can not be used for indexing (t(1.5) will not work). Additionally t (at this point) is not a vector anymore, so anything other than t(1) will throw out an error
disp(['The change time estimate: (', num2str(change_time),')'])
return % this probably does not do what you think it might do
end
t=t+1; %omit this the for loop is incrementing on its own
Folakemi Omotoye
2018-8-7
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
2018-8-7
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).
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)