Logical Indexing: Using a 1x18 array to build a 200*18 matrix

1 回表示 (過去 30 日間)
Stephen
Stephen 2019 年 3 月 30 日
コメント済み: Stephen 2019 年 3 月 30 日
Hello,
Failmatrix is a 200x18 binary matrix. Costmatrix is a 1x18 array of cost integers.
I am trying to create a 200x1 matrix where each row is the combined total of the costmatrix values where failmatrix ==1. If that makes sense.
The following does not produce an error message, however rebuildcost stays at zero.
for i =1:length(failmatrix(:,1))
rebuildcost = costmatrix(failmatrix(i,:)==1);
rebuildcost = sum(rebuildcost);
end
Can anyone advise? Thank you.

採用された回答

Guillaume
Guillaume 2019 年 3 月 30 日
Simply:
rebuildcost = sum(failmatrix .* costmatrix, 2);
  1 件のコメント
Stephen
Stephen 2019 年 3 月 30 日
Wow, very simple, I need to keep practicing haha
Thank you again Guillaume.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCell Arrays についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by