How can i do apply operator componentwice in any vector in N dimensional space? Mat lab code for any vector in N dimension

1 visualizzazione (ultimi 30 giorni)
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?

Risposta accettata

Star Strider
Star Strider il 27 Ott 2018
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.

Più risposte (2)

Stephen23
Stephen23 il 27 Ott 2018
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

Walter Roberson
Walter Roberson il 27 Ott 2018
discretize(v,[-inf,-10, 10,inf]) - 1
or
[~,bin] = histc(v,[-inf,-10, 10,inf]);
bin - 1
  2 Commenti
Steven Lord
Steven Lord il 27 Ott 2018
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.

Accedi per commentare.

Categorie

Scopri di più su Asynchronous Parallel Programming in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by