conditional statements on matrices
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
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.
Voss
2024 年 4 月 8 日
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?
Voss
2024 年 4 月 8 日
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!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
