How to change specific numbers to 0
Show older comments
So here is my current code:
A = [ 1 2 3 4 5 6 7 8 9];
B = [ 1 2 3 4];
A(~B)=0;
disp(A);
Basically I want A to be equal to [ 1 2 3 4 0 0 0 0 0 ]. But for some reason it won't take ~B, where as it will work when it is just B. Anyone know a solution to this or why this is the case? Thanks in advance!
2 Comments
"But for some reason it won't take ~B, where as it will work when it is just B. "
Compare:
B = [1,2,3,4]
~B
The first is a numeric array, the second is a logical array. Now revise what kind of indexing MATLAB supports:
William
on 17 Jun 2023
Accepted Answer
More Answers (1)
John D'Errico
on 16 Jun 2023
Edited: John D'Errico
on 16 Jun 2023
0 votes
For some reason? Learn to use ismember instead. What you think makes sense does not work in MATLAB. And that means you need to start back with the tutorials, to learn MATLAB syntax. Sorry. Time to start at the beginning.
2 Comments
William
on 17 Jun 2023
Daniel
on 17 Jun 2023
Regarding starting at the beginning, speaking as a MathWorker, you probably have access to some MATLAB fundamentals courses if you go to matlab.mathworks.com through your university account and click on "Online Training". You may find the MATLAB Onramp and/or MATLAB Fundamentals to be helpful.
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!