フィルターのクリア

Using a column vector of indices to replace values in a matrix

6 ビュー (過去 30 日間)
Teddy Fisher
Teddy Fisher 2019 年 12 月 13 日
コメント済み: sruizpp 2020 年 11 月 18 日
Hello,
I have a column vector, C, (55x1 double) which are the indices of my values of interest in Matrix, M which is 370x29 double.
I want to replace all the values in M that are not these values of interest with 0.
I am trying to use C to index into M and replace ~C with 0.
However when I try
M(~C)=0
all it does is change all the values in M to just different values and I'm not sure why.
When I try to create a new Matrix
N=M(~C==0) it returns a columnn vector with just the first 55 values in the first column of M.
Does anyone know how I can replace all the elements in M that are not listed in C with 0? I want my output to be another 370x29 double matrix.
Thanks!
  3 件のコメント
Teddy Fisher
Teddy Fisher 2019 年 12 月 13 日
I had another Matrix, R, that was 0s and 1s, also 370x29 double
I used
C=find(R) to get the indexes of the 1s in R (which correspond to the values I want in M)
So the values in C are numbers (between 1-10730), not [row,column] format
Is there any way I can use C to index into M ?
Fangjun Jiang
Fangjun Jiang 2019 年 12 月 13 日
with this comments, the answer below should provide one solution.

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

採用された回答

Fangjun Jiang
Fangjun Jiang 2019 年 12 月 13 日
編集済み: Fangjun Jiang 2019 年 12 月 13 日
C=[1,2,3]';
M=magic(5);
index=setdiff(1:numel(M),C);
M(index)=0
  2 件のコメント
Teddy Fisher
Teddy Fisher 2019 年 12 月 13 日
Thank you so much!! I can't tell you how much this helped
sruizpp
sruizpp 2020 年 11 月 18 日
c00L

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

その他の回答 (1 件)

Guillaume
Guillaume 2019 年 12 月 13 日
編集済み: Guillaume 2019 年 12 月 13 日
Well, now you have explained how you got C, a much simpler method than the accepted one is not to create C and use:
M(~R) = 0; %that's all that is needed!
As usual in matlab, find is completely unnecessary and what you were trying to do initially would have worked had you kept your original logical vector.

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by