Advertisement
tankian202

Untitled

Mar 18th, 2024
1,048
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.74 KB | None | 0 0
  1. function [train_data, d, test_data, d_test] = readTrainingData(distribution)
  2.  
  3. total_files = 10; % Összes fájl száma
  4. total_data_per_file = 1000; % Egy fájlban lévő összes adat
  5. if(distribution>=1)
  6.     distribution = 0.9;
  7.     error("Distribution can't be bigger or equal than 1.")
  8. end
  9.  
  10. train_data_per_file = round(total_data_per_file*distribution);
  11.  
  12. test_data_per_file = round(total_data_per_file*(1-distribution));
  13. train_data = zeros(train_data_per_file*total_files, 785); % Tanító halmaz inicializálása
  14. d = zeros(train_data_per_file * total_files, 10);
  15. d(:) = -1;
  16.  
  17. test_data = zeros(total_files * test_data_per_file, 785); % Teszt halmaz inicializálása
  18. d_test = zeros(total_files * test_data_per_file, 10);
  19. d_test(:) = -1;
  20.  
  21. d_counter = 1;
  22. d_test_counter = 1;
  23.  
  24. for file_index = 0:(total_files - 1)
  25.     file_name = sprintf('data%d', file_index); % Aktuális fájl neve
  26.     fid = fopen(file_name, 'r');
  27.    
  28.     for i = 1:total_data_per_file
  29.         t = fread(fid, [28 28], 'uchar');% Beolvasás
  30.        
  31.         % 2D mátrix átalakítása 1D vektorrá, ahol 0 van a mátrixban, ott is 0 marad, egyébként 1
  32.         t_vector = double(t(:)'./255);
  33.         t_vector_with_ones = [1, t_vector]; % Minden sor elejére beszúrunk egy 1-est
  34.  
  35.         if i <= train_data_per_file
  36.             % Tanító halmazba mentés
  37.             train_data(d_counter, :) = t_vector_with_ones;
  38.             d(d_counter, file_index + 1) = 1;
  39.             d_counter = d_counter + 1;
  40.         else
  41.             % Teszt halmazba mentés
  42.             test_data(d_test_counter, :) = t_vector_with_ones;
  43.             d_test(d_test_counter, file_index + 1) = 1;
  44.             d_test_counter = d_test_counter + 1;
  45.         end
  46.     end
  47.  
  48.     fclose(fid);
  49. end
  50. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement