hello everyone
I want to read data from csv file and classify these data. my csv look like this:
0
0.139999945
0.209999918
12.86283386
0
-0.649996554
-2.609774285
-114.4151
...
its [154*1][row*col], I want to classify these data as
  • (if data = 0 then write 0 in the second column)
  • (if 0<data<=45 then print 1)
  • (if 45<data<=90 then print 2)
  • (if 0>data>=-45 then print 3)
  • (if -45>data>=-90 then print 4)
I don't know what should I write in the condition statement! the following is my attempt but it doesn't give the desired output.
M = csvread('imu.csv')
[rows cols] = size(M);
for k = 1:length(M)
if M == 0
A=0;
elseif 0<M<=45
A=1;
elseif 45<M<=90
A=2;
elseif 0>M>=-45
A=3;
else
A=4;
end
end
csvwrite('imu2.csv',A);

댓글 수: 1

Image Analyst
Image Analyst 2018년 5월 11일
편집: Image Analyst 2018년 5월 11일
"write 0 in the second column" of what , A or M? If A, then what is in the first column of A?

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

 채택된 답변

Star Strider
Star Strider 2018년 5월 11일

0 개 추천

The loop and if block are not necessary.
I would simply do this:
M = randn(10, 1); % Create Vector
M = [M zeros(size(M))]; % Preallocate
M(M(:,1) > 0,2) = 1;
M(M(:,1) < 0,2) = 2;
That initially creates the second column as zeros, then sets the second column according to the positive and negative values in the first column.

댓글 수: 4

Khadija Al Jabri
Khadija Al Jabri 2018년 5월 11일
thank you so much for your replay. its work with your code but I need more class as I edit my question above, could you take a look please? and give me your suggestion!
My pleasure.
The code changes to:
M = [M zeros(size(M))]; % Preallocate
M(( 0 < M(:,1)) & (M(:,1) <= 45 ) ,2) = 1;
M(( 45 < M(:,1)) & (M(:,1) <= 90 ) ,2) = 2;
M(( 0 > M(:,1)) & (M(:,1) >= -45 ) ,2) = 3;
M((-45 > M(:,1)) & (M(:,1) >= -90 ) ,2) = 4;
That does what you want. I kept the previous data name (here ‘M’) for consistency. You are of course free to change it.
Khadija Al Jabri
Khadija Al Jabri 2018년 5월 11일
it's work!
Thank you so much, I appreciate your help.
Star Strider
Star Strider 2018년 5월 11일
As always, my pleasure!

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Statistics and Machine Learning Toolbox에 대해 자세히 알아보기

질문:

2018년 5월 11일

댓글:

2018년 5월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by