Using a column vector of indices to replace values in a matrix
Show older comments
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 Comments
If C is a vector of indices, it's very unclear why you think that ~C would useful for anything ~0 is logical 1 and ~anythingelse is logical 0.
So is C a vector of indices (values came go from 1 to numel(M) == 10730) or is C something else? It would be useful if you told us how you obtained C.
Teddy Fisher
on 13 Dec 2019
Fangjun Jiang
on 13 Dec 2019
with this comments, the answer below should provide one solution.
Accepted Answer
More Answers (1)
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.
1 Comment
Fangjun Jiang
on 13 Dec 2019
indeed!
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!