How to scan a matrix and execute specific commands depending on value.

5 ビュー (過去 30 日間)
Laura Steel
Laura Steel 2022 年 8 月 18 日
編集済み: Walter Roberson 2022 年 8 月 18 日
Let's say I had a 5x2 matrix:
0 1
1 0
1 1
1 0
0 1
I want to scan down the first column, and do the following:
  • If the value is 0, I want to assign a 0 value to a new 5x1 matrix.
  • If the value is >0, I want to move up a row and look at the value in the 2nd column.
  • If the value in this 2nd column is 0, then assign a 0 to the new 5x1 matrix.
  • If the value in this 2nd column is >0, then assign a 1 to the new 5x1 matrix.
  • Continue through the rest of the matrix.
So, the output should look like this:
0
1
0
1
0
Many thanks!
  2 件のコメント
Walter Roberson
Walter Roberson 2022 年 8 月 18 日
What if the value is > 0 in the first column of the first line? "move up a row" would be moving to before the matrix.
Laura Steel
Laura Steel 2022 年 8 月 18 日
Hi, thanks for taking a look! The first value of the first column will always = 0, so this should not be a problem.

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2022 年 8 月 18 日
編集済み: Walter Roberson 2022 年 8 月 18 日
matrix = [0 1
1 0
1 1
1 0
0 1]
matrix = 5×2
0 1 1 0 1 1 1 0 0 1
if matrix(1,1)
output1 = nan; %his case is not defined, requires referring to row above first
else
output1 = 0;
end
output2end = matrix(2:end,1) & matrix(1:end-1,2)
output2end = 4×1 logical array
1 0 1 0
output = [output1; output2end]
output = 5×1
0 1 0 1 0

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeElementary Math についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by