Replace values with zero and zero with values in matrix simultaneously

1 回表示 (過去 30 日間)
Emma Kuttler
Emma Kuttler 2022 年 4 月 15 日
回答済み: John D'Errico 2022 年 4 月 15 日
Hi, I have a 1363x12 matrix called SwRailNodeDemand. The first column is consecutive integers, and the other eleven columns either have values that are either positive, negative, or zero. What I'd like to do is keep the first column as is, but for the other columns, simultaneously replace the zeroes with a value (100) and the nonzero values with zero. I think this has to happen simultaneously in order to not have a matrix of all zeroes.
Then, after these changes have ocurred, I'd like to return only the rows that have nonzero values in columns 2:11.
Here's an example:
SwRailNodeDemand =
[1 0 4 -5 0 6 8 -11 7 9 10 0
2 1 0 0 0 0 0 0 0 2 0 -3
3 3 4 1 1 0 0 0 0 0 0 -1
4 1 3 1 1 1 1 1 1 1 1 1]
NewMatrix =
[1 100 0 0 100 0 0 0 0 0 0 100
2 0 100 100 100 100 100 100 100 0 100 0
3 0 0 0 0 100 100 100 100 100 100 0]
Thanks!

採用された回答

John D'Errico
John D'Errico 2022 年 4 月 15 日
Easy.
SwRailNodeDemand = [1 0 4 -5 0 6 8 -11 7 9 10 0;
2 1 0 0 0 0 0 0 0 2 0 -3;
3 3 4 1 1 0 0 0 0 0 0 -1;
4 1 3 1 1 1 1 1 1 1 1 1];
NewMatrix = [SwRailNodeDemand(:,1),(~SwRailNodeDemand(:,2:end))*100]
NewMatrix = 4×12
1 100 0 0 100 0 0 0 0 0 0 100 2 0 100 100 100 100 100 100 100 0 100 0 3 0 0 0 0 100 100 100 100 100 100 0 4 0 0 0 0 0 0 0 0 0 0 0

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by