How to select the specific rows that I want in matrix using for loop?

조회 수: 1 (최근 30일)
ai ping Ng
ai ping Ng 2017년 5월 6일
답변: Walter Roberson 2017년 5월 8일
I have 150 X 344 matrix, 150 is the file numbers that I need to examine and 344 is my total number of features ('CombineFeatureAll'). I want to undergo 1-50 rows for testing and another 51-150 rows for training. Inside the 1-50 rows, the number smaller than 25 will be 1 else -1. I know by coding like this size(CombineFeatureAll,1) will get all the rows, but how can I limit it to only 50 rows? Below is my coding for testing part.
%start
for c2 = 1:size(CombineFeatureAll,1)
%for label
if c2<=25
str1=['1' ' '];
else
str1=['-1' ' '];
end
for c1 = 1:size(CombineFeatureAll,2)
str1 =[str1 ['',num2str(c1),':' num2str(CombineFeatureAll(c2,c1)) ' '] ];%feature set
end
dlmwrite('testing.te', str1, 'delimiter', '', 'precision', 6,'-append');
end

답변 (2개)

ai ping Ng
ai ping Ng 2017년 5월 8일
편집: Walter Roberson 2017년 5월 8일
A = CombineFeatureAll(1:50,1:344);
for c2 = 1:size(A,1)
%for label
if c2<=25
str1=['1' ' '];
else
str1=['-1' ' '];
end
for c1 = 1:size(A,2)
str1 =[str1 ['',num2str(c1),':' num2str(A(c2,c1)) ' '] ];%feature set
end
dlmwrite('testing.te', str1, 'delimiter', '', 'precision', 6,'-append');
end
It can be run this way but this maybe not a good method?

Walter Roberson
Walter Roberson 2017년 5월 8일
A = CombineFeatureAll(1:50,1:344);
c2 = [ones(25,1); -ones(25,1)];
t = [c2, A];
dlmwrite('testing.te', A, 'precision', 6);

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by