フィルターのクリア

How to reassign values to new array without using a loop

1 回表示 (過去 30 日間)
Master Yoda
Master Yoda 2021 年 2 月 24 日
コメント済み: Master Yoda 2021 年 2 月 24 日
I am trying to reassign values to a new matrix as a method of cleaning up / bookeeping / decreasing memory. My current code is as follows.
for i = 1:N,
newMat(i,:) = oldMat(dataVec1 == dataVec2(i), :);
end
This loop takes FOREVER to do as N is very large and length(dataVec1) > N. Is there a way to avoid a for loop to achieve this? I have tried using bsxfun, but the intermediate matrix far exceeds memory.

採用された回答

David Hill
David Hill 2021 年 2 月 24 日
[~,idx]=ismember(dataVec2,dataVec1);
newMat=oldMat(idx,:);
  1 件のコメント
Master Yoda
Master Yoda 2021 年 2 月 24 日
Holy cow! Such an elegant solution. Thanks so much!

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by