conditional statements on matrices
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
Hi, I have two matrices like below:

I want to search when the 2nd column equals 0 on either matrix and make a new matrix that shows the 0 values along with its paired column 1 values. I also want to tag it to know from which matrix it came from.
Thank you! I am new to conditional statements.
채택된 답변
Voss
2024년 4월 8일
Where M1 and M2 are your matrices:
idx1 = M1(:,2) == 0;
idx2 = M2(:,2) == 0;
tags = repelem([1; 2],[nnz(idx1) nnz(idx2)]);
result = [[M1(idx1,:); M2(idx2,:)] tags]
댓글 수: 9
puccapearl
2024년 4월 8일
Voss, you've done it again. I appreciate the simple solution and was over thinking it by thinking I had to use conditional statements/loops.
Many thanks!
Voss
2024년 4월 8일
You're welcome!
puccapearl
2024년 4월 8일
Hi Voss, I'm getting a larger matrix than desired for some reason. The matrix length of 0s for M1 is 47,582 and M2 433,660 so combined should have a 909,552 by 3 matrix but I'm getting 141,648 by 3 matrix.
Any suggestions?

Voss
2024년 4월 8일
Save M1 and M2 in a mat file and upload it here using the paperclip button.
load M1
load M2
whos M*
Name Size Bytes Class Attributes
M1 525600x2 8409600 double
M2 525600x2 8409600 double
I'm not sure why you say "the matrix length of 0s for M1 is 47,582 [475,892?] and M2 433,660". How are you determining that?
I get 49,708 zeros in the second column of M1 and 91,940 zeros in the second column of M2:
idx1 = M1(:,2) == 0;
idx2 = M2(:,2) == 0;
nnz(idx1)
ans = 49708
nnz(idx2)
ans = 91940
49708+91940 = 141648, exactly the size of the result you get.
puccapearl
2024년 4월 8일
thats interesting, if I do
x = nnz(idx2);
from your code I also get 91940 but if I do
x = nnz(M2(:,2));
I get 433660
** I am guessing what I did was incorrect/invalid?
You're basically counting the opposite of what you want. (Notice that 433660 (your count) + 91940 (my count) = 525600 (the total number of elements in M(:,2).)
This
nnz(M2(:,2))
gives you the number of non-zero elements in column 2 of M2. (nnz means number of non-zero elements.)
But you want to count the number of zero elements, which is
idx2 = M2(:,2) == 0;
nnz(idx2)
or
nnz(M2(:,2) == 0)
because idx2 (or M2(:,2) == 0) is a logical vector that is true where M(:,2) is zero and false where M(:,2) is non-zero. In a logical array, false is considered zero and true is non-zero. So nnz(idx2) is the number of non-zero elements in idx2, which is the number of elements where idx2 is true, which is the number of elements where M(:,2) is zero.
puccapearl
2024년 4월 8일
oh my gosh! *face palm*
Thank you! I'm sorry for the run around!
Voss
2024년 4월 8일
No problem! You're welcome!
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
참고 항목
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)
