loop through a column and display the number of elements until the same value occcurs

Hi,
how am I able to loop through a coulmn and get as the output the numbers of rowselement between the first and the next element which is equal to the first element. Here is an example, if this vector:
1
2
3
4
5
7
9
2
1
5
6
8
9
2
4
5
6
now I would like to loop through the column and display the number of elements between the two numbers which are equal.
I tried to use something like this: for i:numel(matrix_call)
for n:
if matrix(i,2)==matrix(i+n,2)
but I am not sure how to do that.
The output for the example I mentioned above should be a new vector which displays in which row the next element, which is equal to the one we are looking at, is following, such as:
vector:
9 (row in which the one is occuring for the second time)
8 (in row 8 there is the number "2" for the second time)
NaN (there is no second number "3")
15 (number "4" occcurs in line 15 for the second time)
and so on

 Risposta accettata

This gives the number of element between two consecutive same numbers.
EDIT
A=[1 2 3 4 5 7 9 2 1 1 5 6 8 9 2 4 5 6]'
m=numel(A);
out=nan(1,m);
numb=out;
for k=1:m
B=A(k:end);
idx=find(B==B(1),2);
n=numel(idx);
if n>1
out(k)=idx(2)-idx(1)+k;
numb(k)=numel(unique(B(idx(1)+1:idx(2)-1)));
end
end
[A out' numb']

18 Commenti

thanks! it's working, no I have to do it with the effective data I ve got, but I should be possible for me to do
do you know why it's not working when I paste your code into a script und try to run the script?
Add these lines of code to the beginning of your code (pre-allocate)
out=zeros(1,numel(A));
numb=out;
I'll post you the error message when matlab is finished with computing.
I entered the code above for a double matrix and matlab is still computing even though I have started it about 30 minutes ago, do you have any idea how long that will take?
What is the size of your array,
Also you should pre-allocate
out=zeros(1,numel(A));
numb=out;
the size of the matrix is 9880x8
I will do so afterwards
ah now I know why, I didn't intend to run the loop for all columns but only for column 2
entering matrix (i,2) instead of vector A as before would run the for-loop only for column 2 of the matrix (which is called matrix as well), is that correct?
This should be much faster
A=[1 2 3 4 5 7 9 2 1 1 5 6 8 9 2 4 5 6]'
m=numel(A);
out=nan(1,m);
numb=out;
for k=1:m
B=A(k:end);
idx=find(B==B(1),2);
n=numel(idx);
if n>1
out(k)=idx(2)-idx(1)+k;
numb(k)=numel(unique(B(idx(1)+1:idx(2)-1)));
end
end
[A out' numb']
if I paste that in a script and run the script with F5 it says: Undefined function 'next' for input arguments of type 'char'.
but if I paste it into the command window it's working. unfortunately there are several steps I am not really sure what the program does, could you give me a short description?
thanks a lot, I really appreciate you helping me
It does not - you must have some extra lines in there that are not in Azzi's code (I checked by copying and pasting myself). Though I agree that comments would certainly improve the code.
%A=[1 2 3 4 5 7 9 2 1 1 5 6 8 9 2 4 5 6]'
% pre-allocate out=[nan nan ... nan], numb=out
%-------------------------------------------------
% for k=1
% B=A(1:end)=A
% idx=find(B==B(1),2)=find(B==1,2)=[1 9]; find the first two 1
% if idx contains two values then out(1)=idx(2)-idx(1)+1=9-1+1=9
% repeat for k=2
% for k=2
% B=A(2:end)=[2 3 4 5 7 9 2 1 1 5 6 8 9 2 4 5 6]'
% idx=find(B==B(1),2)=find(B==2,2)=[1 7]; find the first two 2
% if idx contains two values then out(1)=idx(2)-idx(1)+1=7-1+2=9
%and so on
you're right, I opened a new script and now it's working. is it possible that if a put spaces in a script name, matlab can't execute the code in the script?
however, it would be really great to have a short description, this way I do really get what the program is doing. unfortunately, I am not very experienced with matlab and therefore I am thankfull for every bite I learn is there a way to do the loop above for a whole matrix, based on two criterias? I am now able to do it for the strike of each option and with the code you gave me, it's really fast and everything is working perfectly, but I am not sure how to deal with two criterias. Below there is an example
1160 1
1165 1
1170 1
1175 1
1180 1
1185 1
1190 1
1195 1
1160 2
1165 2
1170 2
1175 2
1180 2
1185 2
1190 2
1195 2
1160 1
1165 1
1170 1
1175 1
1180 1
1185 1
1190 1
1195 1
1160 2
1165 2
1170 2
1175 2
1180 2
1185 2
1190 2
1195 2
the new vector I am looking for should display when both, the first line and the second line are equal
When you put a space in the name, it gives another name. I did not understand your new question, I also suggest to post a new question and make it as clear as possible, post a sample of your data and what should be the result.
is there a way to post a sample when entering a question here? How? Up to now I have always entered the elements by myself which isn't really efficient
I mean post a small part of your data, like you did it in the above question, also add what should be the result. To get more chance to have answers, post a new question.
this I understood but I am asking if there is an easy way to post that data in the forum here. if I copy paste it, id doesn't work, I always have to enter more spaces etc. and that's why I am asking if there is a way to include a screenshot or something like this
You can post a link where you will upload your data file. Posting an image showing your data is not useful because we can't copy it.
how can I post a link/ how am I able to upload the data anywhere so I am able to post a link? I have posted another question including the data, I hope it is clearer what I am up to

Accedi per commentare.

Più risposte (1)

A=[1 2 3 4 5 7 9 2 1 5 6 8 9 2 4 5 6]'
for k=1:numel(A)
B=A(k:end)
idx=find(B==B(1))
n=numel(idx)
if n==1
out(k)=nan
else
out(k)=idx(2)-idx(1)+k
end
end

1 Commento

yes it is possible, in fact the same number occcurs more than 100 time and I need a vector to see where the next number which is equal to the one I am looking for is occuring. as a alternative, it woudl also be possible, I think, to create a matrix which shows the numbers of elements between the first time the numbers are equal, between 2nd and 3rd occurence etc.

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by