loop through a column and display the number of elements until the same value occcurs
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
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
1 Comment
Azzi Abdelmalek
on 17 Mar 2013
Is it possible that an element can be repeated more then twice?
Accepted Answer
Azzi Abdelmalek
on 17 Mar 2013
Edited: Azzi Abdelmalek
on 17 Mar 2013
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 Comments
Locks
on 17 Mar 2013
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?
Azzi Abdelmalek
on 17 Mar 2013
post error message
Azzi Abdelmalek
on 17 Mar 2013
Edited: Azzi Abdelmalek
on 17 Mar 2013
Add these lines of code to the beginning of your code (pre-allocate)
out=zeros(1,numel(A));
numb=out;
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?
Azzi Abdelmalek
on 17 Mar 2013
Edited: Azzi Abdelmalek
on 17 Mar 2013
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']
Locks
on 17 Mar 2013
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
Image Analyst
on 17 Mar 2013
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
Locks
on 17 Mar 2013
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
Azzi Abdelmalek
on 17 Mar 2013
Edited: Azzi Abdelmalek
on 17 Mar 2013
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.
Locks
on 17 Mar 2013
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
Azzi Abdelmalek
on 17 Mar 2013
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.
Locks
on 17 Mar 2013
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
Azzi Abdelmalek
on 17 Mar 2013
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.
Locks
on 17 Mar 2013
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
Image Analyst
on 18 Mar 2013
More Answers (1)
Azzi Abdelmalek
on 17 Mar 2013
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 Comment
Locks
on 17 Mar 2013
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.
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)