Iterating through a dataset and creating a vector with according values

So I have a 10 000 X 1 vector (let's call it vector A) of different values ranging from -1 to 1. I want to create another 10 000 X 1 vector (let's call it vector B) that has values corresponding to the ones in the first vector. I want to create a loop that does this:
Everytime a value in vector A is bigger than 0.5, add the value 3 to vector B with the same corresponding position.
Everytime a value in vector A is smaller than -0.5, add the value 1 to vector B with the same corresponding position.
Everytime a value in vector A is between -0.5 and 0.5, add the value 2 to vector B with the same corresponding.
In that case, the resulting matrix should look like this:
[A] [B]
-0.9 1
-0.3 2
0.6 3
-0.2 2
-0.7 1
I know I need to use a loop but I struggle with this. Thank you so much for your help.

2 Comments

What about border values i.e. 0.5 and -0.5, where value will you assign to corresponding B elements?
0.5 should be included in 3 and -0.5 should be included with 1

Sign in to comment.

Answers (1)

Rarely need loops for such things as this with MATLAB; either logical addressing or table lookup almost always comes to the rescue -- the latter (using the builtin interpolation routine) solution here would be
A=[-0.9;-0.3;0.6;-0.2;-0.7];
B=interp1([-1,-0.5,0.5,1],[1,2,3,3],A,'previous')
B = 5×1
1 2 3 2 1
Alternatively, another lookup solution, with less overhead...
B=discretize(A,[-1,-0.5,0.5,1])
B = 5×1
1 2 3 2 1

4 Comments

Thank you, it does work with the interpolation routine.
Now, following the same logic, how can I create another vector that has string values in them instead of 1 2 and 3? If the number is 1, I want the string to be 'GT', if the number is 2, I want the string to be 'IN' and if the number is 3, I want the string to be 'ST'.
@dpb, your answers will not give the answer as expected by OP
A=[-0.9;-0.3;0.6;-0.2;-0.5;0.5];
%border values
% -0.5 =>1 and 0.5 => 3
B=interp1([-1,-0.5,0.5,1],[1,2,3,3],A,'previous')
B = 6×1
1 2 3 2 2 3
B=discretize(A,[-1,-0.5,0.5,1])
B = 6×1
1 2 3 2 2 3
Direct logical indexing might be a good option
C=1*(A<=-0.5)+2*(A>-0.5&A<0.5)+3*(A>=0.5)
C = 6×1
1 2 3 2 1 3
After you get the values, you can get the values as such
str={'GT';'IN';'ST'};
A=[-0.9;-0.3;0.6;-0.2;-0.5;0.5];
B=interp1([-1,-0.5,0.5,1],[1,2,3,3],A,'previous');
%cell array
C=str(B)
C = 6×1 cell array
{'GT'} {'IN'} {'ST'} {'IN'} {'IN'} {'ST'}
%char array
D=cell2mat(C)
D = 6×2 char array
'GT' 'IN' 'ST' 'IN' 'IN' 'ST'
If the border edge cases are that important, use bkpt-eps(bkpt) to add the necessary granularity in either solution.
There are multiple other answers I've posted (at least one pretty recently) that illustrate...

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 20 Sep 2022

Edited:

dpb
on 20 Sep 2022

Community Treasure Hunt

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

Start Hunting!