필터 지우기
필터 지우기

How to vectorize ?

조회 수: 1 (최근 30일)
Arif Hoq
Arif Hoq 2022년 11월 3일
답변: Walter Roberson 2022년 11월 3일
according to my vectorization the output should be = [0 ; 1 ; 0 ; 1 ; 0; 1]. What am I missing ?
a=[600 300 510 250 700 300]';
a(a >=520)=0;
a(a<=500)=1;
a(a >500 & a <520)=0;
output=a
output = 6×1
1 1 0 1 1 1
If I use loop, then it's fine
a=[600 300 510 250 700 300]';
for i=1:6
if a(i) >520
a(i)=0;
elseif a(i) <500
a(i)=1;
else
a(i)=0;
end
end
output=a
output = 6×1
0 1 0 1 0 1

채택된 답변

DGM
DGM 2022년 11월 3일
편집: DGM 2022년 11월 3일
You're overwriting the original values, so subsequent tests on the array will produce unexpected matches. Both 0 and 1 are <500.
You could do this by simply not working on the same array. Note also that in this case, the test simplify.
a=[600 300 510 250 700 300]';
output = a<=500
output = 6×1 logical array
0 1 0 1 0 1
If you were intending to assign different values for each test case, you could still do that.
a=[600 300 510 250 700 300]';
output = zeros(size(a));
output(a>=520) = 1;
output(a<=500) = 2;
output(a>500 & a<520) = 3
output = 6×1
1 2 3 2 1 2
  댓글 수: 2
Arif Hoq
Arif Hoq 2022년 11월 3일
편집: Arif Hoq 2022년 11월 3일
Thank you very much. I am aware of this. But, is there any way to allow all these 3 conditions (a>=520 , a<=500 and 520>a>500) in a vectorized way ? As usual, output is expected to Binary 0 or 1.
DGM
DGM 2022년 11월 3일
편집: DGM 2022년 11월 3일
Consider that the assignments and individual tests are vectorized. If the question is whether you can perform all three tests with a single comparison against a vector of thresholds, the answer is "maybe, but why?". In order to do that, you'd have to write all four comparisons in a way that allows for the same relational operator to be used in all tests. Can you do that? Would it impact the complexity of the surrounding combinatorial logic? Would it still be readable?
If the question is whether they can just be combined into a common expression, they can. Again, it simplifies to a single test.
a = (480:10:540)';
output = ~(a>=520) & ~(a>500 & a<520) & (a<=500); % this is the saem
output2 = a<=500; % as this
[a output output2]
ans = 7×3
480 1 1 490 1 1 500 1 1 510 0 0 520 0 0 530 0 0 540 0 0

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2022년 11월 3일
output = 1*(a>=520) + 2*(a<=500) + 3*(a>500 & a<520)
Note: output will be 0 for any location that fails all three tests.
For finite real numbers, logically every number should fall into one of the three categories. When we extend to +inf and -inf, the tests will also succeed, calculating 1 for +inf and 2 for -inf -- so the test works for finite reals and for +inf and -inf.
Where can the test fail for real numbers? Answer: it can fail for nan . nan compared with anything is always false no matter what the other thing is (including nan). Well, except for the ~= case, since nan~=nan is true, since that is logically ~(nan == nan) and the == is false and ~false is true.
I mentioned real numbers. What about complex numbers? Well it happens that the < and <= and > and >= operators ignore imaginary parts, so for the purposes of the above expression if you have complex inputs, the expression above would effectively be the same as
output = 1*(real(a)>=520) + 2*(real(a)<=500) + 3*(real(a)>500 & real(a)<520)

카테고리

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

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by