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
댓글 수: 1
Is it possible that an element can be repeated more then twice?
채택된 답변
Azzi Abdelmalek
2013년 3월 17일
편집: Azzi Abdelmalek
2013년 3월 17일
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
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?
post error message
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
Azzi Abdelmalek
2013년 3월 17일
편집: Azzi Abdelmalek
2013년 3월 17일
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
추가 답변 (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
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.
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
참고 항목
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)
