I have a vector in matlab
a = [1 8 0 7 0 5 9 0 0 0 0 0 0 0 0]
Here I am interested to FIND the first index (beyond which the value are completely zero) where the zeros occur continuously. In this example I expect the answer to be 8.
find pretty much does this for you:
find(a, 1, 'last') + 1
since find just returns a list of the positions of non-zero characters, all you have to do is ask find to only give you the last such element and then the next element (hence the +1)
One approach that works even if your last entry is non-zero or your first entry is zero or all your entries are zero, covers just about everything.
find(diff([1 a]==0)==1,1,'last')
Note that this finds the location of the last group of zeros.
last_idx = max(find(a~=0)) + 1
however, if your last entry is not a zero you've to be careful...
Related
I am calculating ENSO indices using Matlab and one condition is that I have to find anomalous sea surface temperatures. The condition is that an El NiƱo event is characterised by sea surface temperatures that are 0.5 degrees above the normalised "0-value" for 5 months. I have gotten as far as to make my monthly time series data logical (i.e. "1" is a monthly data value above 0.5 and "0" is a monthly data value below 0.5), but I wanted to know if there was a command in Matlab that allows me to identify when this value repeats 5 times or more.
As an example code:
Monthly_data=[0 0 1 1 1 1 1 0 0 0 1 1 0 0 0 0 1 0 1 1 1 1 1 1 1 0]
I would ideally need a command that finds when a minimum of five "1"s occur after each other. Does this exist?
If more info is needed please let me know, I am new to matlab so I am not yet sure of the structure and syntax that is valued for asking questions on here.
Thank you!
not sure this is what you need but perhaps gives you some direction.
> x = diff(Monthly_data);
> find(x==-1)-find(x==1)
ans =
5 2 1 7
these are the lengths of the 1 sequences. You may need to pad front and end of the array with 0 to eliminate sequences missing one boundary.
To find the start index of the sequence longer than 5:
> s=find(x==1);
> s(find(x==-1)-s>5)
ans = 18
or
> s(find(x==-1)-s>=5)
ans =
2 18
note that because of the diff lag, these are one more than the array index, or consider it as position for zero based indexing.
covpatt1=covpatt;
covpatt(all(~covpatt1,2),:)=[];
covpatt(:,all(~covpatt1,1))=[];
I tried using these code, covpatt is a matrix, but I don't know what did these code do to covpatt.
Lets take it step-by-step.
~covpatt1 matrix indicates which elements in the covpatt1 matrix are zero. all(X,2) indicates which of the rows of matrix X has all non-zero elements. Therefore, all(~covpatt1,2) indicates which of the rows of covpatt1 have all zeros. Finally, covpatt(all(~covpatt1,2),:)=[]; replaces such rows consisting of all zeros by an empty matrix. That is basically deletion of those rows.
Similarly, for the second statement, covpatt(:,all(~covpatt1,1))=[];, it replaces columns which contains all zeros by an empty matrix i.e. removes those columns.
I think this example clearly illustrates the purpose of the above code:
covpatt=[1 0 2;0 0 0;-3 0 2]
covpatt =
1 0 2
0 0 0
-3 0 2
%your code segment
covpatt1=covpatt;
covpatt(all(~covpatt1,2),:)=[];
covpatt(:,all(~covpatt1,1))=[];
%result
covpatt =
1 2
-3 2
I'm reading some MATLAB trying to pick it up. The line below is probably rather simple but I do not understand it.
I understand length will give me the length of a vector, in this case a vector which is part of a struct, index_struct.data_incl.
The actual value of index_stuct.data_incl at run time is simply 1. What is confusing me is what is inside the brackets i.e. (index_struct.data_incl == 1)? I can't work out what this line is trying to do as simple as it may be!
int_var = length(index_struct.data_incl(index_struct.data_incl == 1));
try this (but think of x as your index_struct.data_incl:):
x = [1 4 5 13 1 1]
length(x(x==1))
ans =
3
It's just counting the number of elements of your x vector that are equal to 1
because x==1 evaluates to [1 0 0 0 1 1] and then using logical indexing x(x==1) evaluates to [1 1 1] whose length is 3;
It could have been written more simply as sum(index_struct.data_incl == 1)
If I dont see the code I can only guess..., but I guess that index_struc.data_incl should be a vector, with length n meaning that you have the option to read until n files, and all the values of the array should be 0 at the begining, and when you read a file you change the corresponding position in the vector index_struc.data_incl from 0 to 1. After some time you can see how many of these files you have read using
int_var = length(index_struct.data_incl(index_struct.data_incl == 1));
because it will give to you the number of 1 in the vector index_struct.data_incl.
I have an array that starts of with zeros and continues into other numbers
I would like to delete the columns in the array that start off with zero but keep the other numbers
example of an column array below:
x= [0 0 0 0 0 2 4 6 8 0 1 2];
Answer of column array would look like
x= 2 4 6 8 0 1 2
I'm using octave 3.4.2/matlab
Thanks
Here is the code:
x = x(find(x~=0, 1):end);
or
x(1:find(x~=0,1)-1) = [];
The find command should work for this.
Assuming your vector is x:
find(x ~= 0)
Will return all indices where x is non-zero. Just grab the first index and go from there to delete all values from 1 to index.
Logical indexing will work just fine in this case: i.e.,
y = x(:,x(1,:)~=0)
will do the job for you. The inner logical comparison, x(1,:)~=0 returns true for every column whose first element is not zero. The indexing operation, x(:,...) selects only those columns for which the logical comparison returned true.
For example:
a=[1 1 0 0 1 1 0 1 1 1 0 0];
Now i want to sum only the ones which are divides by the zeros:
ones=[2 2 3] - That means two ones,then we have 2 zeros which we do not count,then again two ones etc.
How can i do this?
Well, I would suggest finding all places where it switches from 0 to 1 and then finding all places where it switches from 1 to 0, and using those indices to find those lengths. The problem arises at the edges where if the first entry is 1, it doesn't switch to one from zero, and if the last entry is 1, we never find it because nothing switches to 0 at the end. In order to avoid this problem easily, we can add a 0 in the beginning and one at the end. This way we're guaranteed to find each one of those bursts of ones. In essence:
b = [0 a 0];
d = diff(b);
posEdge = find(d==1);
negEdge = find(d==-1);
countOnes = negEdge - posEdge