How to assign one variable value to another variable and sort out the variable with a loop?

Two variables: DataX and Sample Length of the vectors are same [n, 1]. The variable DataX has data values (double) and the variable Sample has 'n' values. The sample variable values are either 0 or 1. The question is, a function needs to be assigned which would hold the data values of DataX and assign to Sample. Furthermore, another array should be defined which would hold the values of DataX and assign the values to the position of Sample which has the value of 1. So, these '1' values with their respective original position containing the data from DataX should be stored in a different array.

 Accepted Answer

output = DataX(Sample==1)
If you want a full working version:
N = 23;
DataX = randi(9,1,N);
Sample = randi([0,1],1,5);
output = DataX(Sample==1)

3 Comments

Thank you for your reply! I would like to add another query if you could help on this too. Sample data, A(n,1);
Data is as follows: 00000111110000000111110000000111110000011110000001111
A set of 1’s after a set of 0’s (the gap varies, it isn’t constant but the order remains same). The desired function is to take the values of first 1 plus two 0 from the end. So the values extracted are: 100100100100 (based on the above sample)
What loop can possibly return this data?
Much appreciated!
>> S = '00000111110000000111110000000111110000011110000001111';
>> C = regexp(S,'100','match');
>> [C{:}]
ans =
100100100100
Thank you for your reply. I guess regexp works only for string. However in my case S is an array [n,1] and the value return should be of the similar type array with each number at a different position.
Output [12,1]:
1
0
0
1
0
0
1
0
0
1
0
0
Also, if I import big data and the range of zeros I want to select is around 1000-1100 (between the '1s', what looping structure can I use?
Thanks!

Sign in to comment.

Categories

Asked:

on 14 Jul 2016

Commented:

on 19 Jul 2016

Community Treasure Hunt

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

Start Hunting!