필터 지우기
필터 지우기

How to change specific numbers to 0

조회 수: 7 (최근 30일)
William
William 2023년 6월 16일
답변: Daniel 2023년 6월 17일
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);
1 2 3 4 5 6 7 8 9
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
Stephen23
Stephen23 2023년 6월 17일
"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 = 1×4
1 2 3 4
~B
ans = 1×4 logical array
0 0 0 0
The first is a numeric array, the second is a logical array. Now revise what kind of indexing MATLAB supports:
William
William 2023년 6월 17일
@Stephen23 thank you for pointing me in a direction that will help my understanding of this code in general

댓글을 달려면 로그인하십시오.

채택된 답변

Daniel
Daniel 2023년 6월 17일
There are two ways you can select elements of an array in MATLAB.
  1. Pass in an array of valid indices. "Valid" means that they're within the range of indices to the array.
  2. Pass in an array of logical values, of the same size as the array.
a = (1:10)*2; % Demo array. This has 10 elements, indexed 1 through 10, values 2, 4, 6, ... 20.
% First method
a(1)
ans = 2
a(10)
ans = 20
a(end) % Special case: "end" refers to the last element in the array
ans = 20
a([5 7 2])
ans = 1×3
10 14 4
% Second method
mod(a,4) == 0 % Notice that this returns a "logical" data type at the output, as will any comparison
ans = 1×10 logical array
0 1 0 1 0 1 0 1 0 1
a(mod(a,4)==0)
ans = 1×5
4 8 12 16 20
~ is a logical operator, so ~B is an array of logicals. Since it's a logical array, it has to have the same size as A, which it doesn't. Therefore the line of code fails.
One alternative method would be to write:
A(5:end) = 0;
That will select and modify elements 5 through 9 (the last element) of A.

추가 답변 (1개)

John D'Errico
John D'Errico 2023년 6월 16일
편집: John D'Errico 2023년 6월 16일
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
William 2023년 6월 17일
Ok, I wasn't going to respnond to you until you came back 30 minutes later on a friday night and edited your response to be condesending. I am an undergrad 21 year old student who just tries to learn this code with no instruction from my school. You have commented on multiple of my questions just being an a jerk and honestly not helpful. I will be the first to tell you I am not good at coding or will claim to be the smartest in the room. But I try ok. I try to be adequete, and sometimes I don't always reach that. But I'm trying. So please, next time you leave a reply on someone's profile just understand that people come from different backrounds and don't always get the same level of teaching.
Daniel
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by