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!