>> x= [ 2 3 4 8 10 0 -2]
x =
2 3 4 8 10 0 -2
>> x(mod(x,2)==0)=5*x(mod(x,2)==0)
x =
10 3 20 40 50 0 -10
can someone explain x(mod(x,2)==0)=5*x(mod(x,2)==0)

 채택된 답변

Star Strider
Star Strider 2022년 3월 19일

0 개 추천

The assignment:
x(mod(x,2)==0)=5*x(mod(x,2)==0)
finds numbers tthat are evenly divisible by 2 (so the remainder after division equals 0) and multiplies them by 5.
So:
x= [ 2 3 4 8 10 0 -2]
x = 1×7
2 3 4 8 10 0 -2
Lv = mod(x,2) == 0 % Select Elements Evenly Divisible By Zero
Lv = 1×7 logical array
1 0 1 1 1 1 1
x(Lv) = 5*x(Lv) % Multiply Them Bu 5
x = 1×7
10 3 20 40 50 0 -10
.

댓글 수: 3

This:
x= [ 2 3 4 8 10 0 -2]
x(mod(x,2)= =0 & mod(x,4)= =0)=100
is slightly more complicated than the original example.
Here, the elements of ‘x’ must be integer divisors of 2 and be integer divisors of 4 to be considered, and then assigned the value of 100:
x= [ 2 3 4 8 10 0 -2]
x = 1×7
2 3 4 8 10 0 -2
Lv = (mod(x,2)==0 & mod(x,4)==0)
Lv = 1×7 logical array
0 0 1 1 0 1 0
x(Lv) = 100
x = 1×7
2 3 100 100 10 100 -2
NOTE — The ‘double equal’ (==) signs must be kept together, not separated by a space. See: MATLAB Operators and Special Characters for details.
.
Faisal Al-Wazir
Faisal Al-Wazir 2022년 3월 19일
thanks
Star Strider
Star Strider 2022년 3월 19일
As always, my pleasure!

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

추가 답변 (2개)

John D'Errico
John D'Errico 2022년 3월 19일

1 개 추천

When something is too big or complicated, TAKE IT APART. TRY IT.
x= [ 2 3 4 8 10 0 -2]
x = 1×7
2 3 4 8 10 0 -2
So x is a vector of integers.
What does mod do?
mod(x,2)==0
ans = 1×7 logical array
1 0 1 1 1 1 1
This takes the modulus of each number, modulo 2. So it returns 0 for even numbers, 1 for odd numbers. What is on the right hand side of the equality? 5*x(mod(x,2)==0)
So what does x(a binary vector as we just created do? TRY IT!!!!!!!
This is a mask operation. It extracts the elements that correspond to 1. So essentially, it extracted oNLY the even elements of x.
x(mod(x,2)==0)
ans = 1×6
2 4 8 10 0 -2
Then what did it do? It multiplied them by 5.
5*x(mod(x,2)==0)
ans = 1×6
10 20 40 50 0 -10
Now what does the equality do there? It tells us to stuff those elements BACK into those same corresponding elements in x.
x(mod(x,2)==0)=5*x(mod(x,2)==0)
x = 1×7
10 3 20 40 50 0 -10
So put it all together. Take each even element of x. Multiply it by 5. Then stuff it back in place.
If something is too complicated, too big for you to understand. then take it apart. Look at only the small pieces. Then put it back together.

댓글 수: 3

can u explain this code for me please ?
x= [ 2 3 4 8 10 0 -2]
x(mod(x,2)= =0 & mod(x,4)= =0)=100
Can't you take it from the answer given ?
The elements of the x vector that are divisible by 2 and by 4 are set to 100, the other elements remain unchanged.
Since each number that is divisible by 4 is also divisible by 2, this is the same as
x(mod(x,4)==0) = 100
John D'Errico
John D'Errico 2022년 3월 19일
Why is this starting to look like a homework ssignment to me?

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

Torsten
Torsten 2022년 3월 19일

0 개 추천

The even elements of the x-vector are multiplied by a factor of 5, the odd elements remain unchanged.

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

질문:

2022년 3월 19일

댓글:

2022년 3월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by