How can i do apply operator componentwice in any vector in N dimensional space? Mat lab code for any vector in N dimension
조회 수: 1 (최근 30일)
이전 댓글 표시
Here is my vector in 100-dimension.
v=
-20
9
-50
90
60
8
16
50
-43
.
.
.
-15
I want to implement operator for each component: i.e if the component is less than -10, take 0. the component is less than between -10 and 10, take 1, the component is greater than or equal to than 10, take 2.
Operating componentwise using this and putting all together as one vector is my problem.
for the above vector, the resulting vector will be
v=
0
1
0
2
2
1
2
2
0
.
.
.
0
How can I code this for an arbitrary vector in N-dimensional vector? in 1000 dimensional, in 200000 dimensional?
댓글 수: 0
채택된 답변
Star Strider
2018년 10월 27일
Try this:
f = @(x) 0.*(x <= -10) + 1.*((x > -10) & (x < 10)) + 2.*(x >= 10); % Anonymous Function
v = randi([-99 99], 20, 1); % Create Vector
Result = [v f(v)] % Vector & Classification
Result =
14 2
69 2
-93 0
28 2
57 2
-86 0
-35 0
2 1
-42 0
-3 1
14 2
-73 0
-43 0
-91 0
25 2
73 2
-9 1
-34 0
39 2
-29 0
Note that this function is vectorised, so it also works for matrices.
댓글 수: 2
Star Strider
2018년 10월 27일
My pleasure.
If my Answer helped you solve your problem, please Accept it!
추가 답변 (2개)
Stephen23
2018년 10월 27일
Simpler:
>> v = [-20;9;-50;90;60;8;16;50;-43]
v =
-20
9
-50
90
60
8
16
50
-43
>> w = (v>=-10)+(v>=10)
w =
0
1
0
2
2
1
2
2
0
댓글 수: 0
Walter Roberson
2018년 10월 27일
discretize(v,[-inf,-10, 10,inf]) - 1
or
[~,bin] = histc(v,[-inf,-10, 10,inf]);
bin - 1
댓글 수: 2
Steven Lord
2018년 10월 27일
When you call discretize, you can specify the values that should be stored in the output instead of the bin number using the third input.
v = 20*randn(10, 1);
v2 = discretize(v, [-Inf, -10, 10, Inf], [73 42 -999]);
[v, v2]
Elements in v between -Inf and -10 will have 73 in the corresponding element of v2 and similarly for the second and third bins.
Walter Roberson
2018년 10월 27일
Thanks, Steven. That would give us
discretize(v,[-inf -10 10 inf],[0 1 2])
which might be clearer to read.
참고 항목
카테고리
Help Center 및 File Exchange에서 Asynchronous Parallel Programming에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!