Converting values to 0 and 1 and then counting occurrences!

조회 수: 26 (최근 30일)
adi kul
adi kul 2015년 5월 26일
편집: Andrei Bobrov 2015년 5월 27일
Hello, I am stuck with a problem.
I have a 200x1 matrix containing values ranging from 0 to 30.
Now I want to convert the values which are less than 20 to zero and values greater than zero to 1.
After doing that I want to count how many occurrences are there when 1 is followed by zero and give the counting as an output!
  댓글 수: 1
adi kul
adi kul 2015년 5월 26일
More information:
I am having 5 .mat files. Each mat file contains 5 parameters. A,B,C,D,E. The 5 files are named Trial1.mat, ....., Trial5.mat
I want to take B variable which is of a size 200x1 from each mat file, convert values less than 20 to 0 and greater than 20 to 1.
0 shows system is OFF 1 Shows system is ON
Now I want to check how many times 1 is followed by 0.
I want to count this occurrence for each .mat file and give an output file which will give number of count per mat file.
Here is the for loop available to put the code:
myFolder = 'C:\Users\adi\Documents\MATLAB\';
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, '*.mat');
matFiles = dir(filePattern);
for k = 1:length(matFiles)
end
I hope now you got my question!

댓글을 달려면 로그인하십시오.

채택된 답변

Thorsten
Thorsten 2015년 5월 26일
A(A<20) = 0;
A(A>0) = 1;
cnt = sum(diff(A) == -1);
  댓글 수: 6
Thorsten
Thorsten 2015년 5월 26일
편집: Thorsten 2015년 5월 26일
The name of the matfiles is stored in your variable MatFiles. The count of occurrences is stored in cnt.
In your question you specified: Now I want to convert the values which are less than 20 to zero and values greater than zero to 1.
This is a bit strange since values between 20 and 0 are mapped to 0 or 1, depending on which rule you use first. Also, a value of 20 would be left as is and not mapped at all. It would probably make more sense to define a treshold T = 20 (or some other value) and then use
B(B < T) = 0;
B(B >= T) = 1;
adi kul
adi kul 2015년 5월 27일
This helped but can we count only change??
I mean instead of 0 followed by 1 there are some 1 followed by zero at the end which are not being counted. So what to do to count total 0 to1 and 1 to 0 changes???

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Sebastian Castro
Sebastian Castro 2015년 5월 26일
편집: Sebastian Castro 2015년 5월 26일
Suppose your original data is named x. The following line will create a matrix the same size as your original, where the elements are 1 if the logical condition is met and 0 otherwise:
xThresh = x>=20
Then, you can use the diff function to get the difference between consecutive elements. You know that a transition from 1 to 0 is a difference of -1, so you can find all the -1s and add them up:
xDiff = diff(xThresh)
numTransitions = sum(xDiff==-1)
- Sebastian
  댓글 수: 2
Image Analyst
Image Analyst 2015년 5월 26일
I recommend (and voted for) Sebastian's straightforward single-step way, over the other two 2-step solutions offered, though they will also work. In addition to being shorter and more direct, this thresholding method doesn't change your original matrix.
To process a bunch of files, like 5 mat files, use one of the two code snippets in the FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F
adi kul
adi kul 2015년 5월 27일
I tried this. But problem is, when there is not "B" column available, it still gives me some value!!

댓글을 달려면 로그인하십시오.


Andrei Bobrov
Andrei Bobrov 2015년 5월 26일
편집: Andrei Bobrov 2015년 5월 27일
out = numel(strfind(A(:)' >= 20,[1, 0]));

Ingrid
Ingrid 2015년 5월 26일
is this what you mean as it is not clear to what end you would like to do this
x(x<20) = 0;
x(x>=20) = 1;
temp = diff(x);
answer = sum(temp == -1);

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by