How to change specific numbers to 0
古いコメントを表示
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 件のコメント
"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
2023 年 6 月 17 日
採用された回答
その他の回答 (1 件)
John D'Errico
2023 年 6 月 16 日
編集済み: John D'Errico
2023 年 6 月 16 日
0 投票
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 件のコメント
William
2023 年 6 月 17 日
Daniel
2023 年 6 月 17 日
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.
カテゴリ
ヘルプ センター および File Exchange で Exploration and Visualization についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!