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

7 次查看(过去 30 天)
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.

采纳的回答

Stephen23
Stephen23 2016-7-14
编辑:Stephen23 2016-7-18
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 个评论
automycer
automycer 2016-7-19
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!

请先登录,再进行评论。

更多回答(1 个)

Azzi Abdelmalek
Azzi Abdelmalek 2016-7-14
out=DataX(logical(Sample))

类别

Help CenterFile Exchange 中查找有关 Cell Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by