how to use for loop?
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
if we have an array a= [1,2,3,1,2,3,4,1,2] and second array b=[1,2]
we want to know what is the number of occurrences that b are in a
which means the answer should be 3.
then how to write that using for loop?
채택된 답변
Star Strider
2019년 1월 1일
0 개 추천
Unless you absolutely must use a loop, use the strfind (link) function:
a = [1,2,3,1,2,3,4,1,2];
b = [1,2];
c = strfind(a,b);
Result = numel(c)
producing:
Result =
3
댓글 수: 12
aya qassim
2019년 1월 1일
if I want to use the "for" is it possible?
because when I tried to do so but matlab warned that the matrix deminisions are not true
I did not understand why
It is. You must compare 2 consecutive elements of ‘a’ to ‘b’ in each iteration of the loop.
Try this:
a = [1,2,3,1,2,3,4,1,2];
b = [1,2];
for k = 1:numel(a)-1
z(k,:) = a(k:k+1) == b;
end
Result = nnz(z(:,1)) % Using A ‘for’ Loop
producing:
Result =
3
Note that you can replace the nnz call with a sum call.
thank you fis of all;
for i=1:numel(x)-1;
z(i:1)=x(i:i+1)==y;
end
r=sum(z(:,1));
I entered x=[1,2,1,2,1,2,1,2,1,2,1] and y=[1,2,1,2,1]
the answer should be 4
but matlab said:
Matrix dimensions must agree
I don't understand
You must change my code to accommodate the length of the vector you want to compare, here ‘y’.
This works:
x=[1,2,1,2,1,2,1,2,1,2,1];
y=[1,2,1,2,1];
for i=1:numel(x)-numel(y)+1 % Limits Must Accommodate The Length Of ‘y’
z(i,:)=x(i:i+numel(y)-1)==y; % The Comparison Must Consider Segments Of ‘x’ Equal To The Length Of ‘y’
end
r=sum(z(:,1))
producing:
r =
4
aya qassim
2019년 1월 1일
why did you put (i+4) what the 4 mean I don't understand
Star Strider
2019년 1월 1일
I edited my previous Comment to make it more general. The current code should address your concerns.
aya qassim
2019년 1월 1일
thank you, but the code did not work it gives me an error answer.
Star Strider
2019년 1월 1일
As always, my pleasure.
What error did it throw?
It worked when I ran it. Please post the code you used. Also, what MATLAB version (release) are you using?
aya qassim
2019년 1월 2일
편집: aya qassim
2019년 1월 2일
I have the version of matlab R2017b
for i=1:numel (x)-numel(y)+1
z(i:1)=(x(i:i+numel(y)-1)==y);
end
a=sum(z(:,1));
x=[1,2,1,2,1,2,1,2,1,2,1]
y=[1,2,1,2,1]
matlab answer:
error, subscripted assignment dimensions mismatch.
thank you,
Your ‘z’ assignment subscrpting is incorrect.
Your code:
z(i:1)=(x(i:i+numel(y)-1)==y);
Your code will either assign the right-hand-side of the equation (a logical vector) to a single scalar (if i=1) or to an empty array (if i>1), neither of which will work.
My code:
z(i,:)=x(i:i+numel(y)-1)==y;
My code assigns the right-hand-side to a matrix row. It works. Use my code.
Please see the documentation section on Matrix Indexing (link) to understand the difference between my code (that works) and your code (that errors), and how to index a matrix correctly.
aya qassim
2019년 1월 2일
Thank you, I understood my mistake.
Star Strider
2019년 1월 2일
As always, my pleasure.
추가 답변 (0개)
카테고리
도움말 센터 및 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)
