필터 지우기
필터 지우기

How to extract the fourth place of decimal of a number which is basically a flag-status?

조회 수: 1 (최근 30일)
I have a multicolumn datafile where the last coloun presents numbers like 3.2341, 2.3450, 1.2320, 8.9871. The fourth place of decimal is always either 1 or 0. If it is 1, it represents a certain flag to be 'ON', otherwise, it is 'OFF'. How to extract this flag information from these numbers?

채택된 답변

Guillaume
Guillaume 2015년 1월 26일
Read your datafile however you want (using textscan, or csvread, or dlmread, or readtable) into a matrix. For example,
m = cell2mat(textscan(fid, '%f %f %f %f')); %or whatever the format is
Multiply the last column of your matrix by 10000, round it just to be sure, and take the modulo with 2 and you get your flag:
m = [1 2 3 3.2341; 4 5 6 2.3450; 7 8 9 1.2320; 10 11 12 8.9871]; %for example
flag = mod(round(m(:, end) * 1e4), 2)
  댓글 수: 2
TP Das
TP Das 2015년 2월 13일
I eventually solved this by converting the number to string.
Guillaume
Guillaume 2015년 2월 13일
That's fine if speed is not critical. Converting to string is going to be a lot slower than just multiplication and modulo.

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

추가 답변 (0개)

카테고리

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