How can i define this in matlab
Show older comments
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
Please explain what you want to achieve.
Read also here:
Best regards
Stephan
Folakemi Omotoye
on 6 Aug 2018
The code
s(-1)
Tries to access the value saved in s at index -1. This is not possible. Please give more context and explain what you want to achieve.
Folakemi Omotoye
on 6 Aug 2018
Edited: Image Analyst
on 6 Aug 2018
Stephan
on 6 Aug 2018
You really make helping hard... Please read the tutorial at the link i posted.
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
on 7 Aug 2018
Answers (1)
Image Analyst
on 6 Aug 2018
0 votes
18 Comments
Folakemi Omotoye
on 6 Aug 2018
Image Analyst
on 6 Aug 2018
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
on 6 Aug 2018
Walter Roberson
on 6 Aug 2018
When you use a logical in a numeric context it is treated as a double in most cases.
Folakemi Omotoye
on 6 Aug 2018
Image Analyst
on 6 Aug 2018
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
on 6 Aug 2018
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
on 7 Aug 2018
Folakemi Omotoye
on 7 Aug 2018
Edited: Image Analyst
on 7 Aug 2018
Dennis
on 7 Aug 2018
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
on 7 Aug 2018
Edited: Image Analyst
on 7 Aug 2018
Image Analyst
on 7 Aug 2018
You might look at cumsum():
s = cumsum(v)
Folakemi Omotoye
on 7 Aug 2018
Dennis
on 7 Aug 2018
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
on 7 Aug 2018
Dennis
on 7 Aug 2018
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
on 7 Aug 2018
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).
Categories
Find more on Numeric Types in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!