2017-04-09 2 views
0

私はN行M列のテーブルからなるcsvファイルを持っています。各行が6つの数字で構成され、私はこれは私はそれで書か文字列を比較して削除する

valid=true(height(Information),1); 
for i=1:height(Information),1; 
    if Information{i, 1} == Information{:, 1} 
     fprintf('Invalid number in line %d', i); 
     valid(i)=false; 
    end 
end 

答えて

0

なければならない最初の読み取り考えどのようなエラーメッセージ を印刷次に数字のいずれかが同一であるかどうかを検出する必要があり、最初のcolumで unique

uniqueVals = unique(A(:,1));% find unique values of col 1 
    [r c] = size(uniqueVals);% r determines the number of unique values in A(:,1) 
    [rr cc] = size(A);% rr is total number of values in A(:,1) 
    if (r~=rr) 
     disp('identical numbers detected'); 
    end 
+0

私はこれを動作させることはできません。 列内の同じ値を検出せず、それを削除してエラーメッセージを表示します – Ryan

+0

私にエラーメッセージを送ってください。 – Javidan

+0

私はproporlyそれを記述する方法がわからない、それはめちゃくちゃされませんので VAR1 VAR2 VAR3 VAR4 VAR5 Var6 'S123456' 'Bnは' 7 2 10 12 'S163765' 'アヤン' 7 10 10 12 'S185216 'Jyan 4' 2 -3 12 ' S854789 'ギャン' 7 2 7 7 'S325874 'Zyan' 7 2 10 2 ' S963256 'Byan' 12 2 10 12 'S123456' '名前 '7 2 4 4 ' S214789 '' Hyan '10 7 10 12 – Ryan

1

使用する第3の出力とhistcounts

% generate two matrices, one with 2 identical elements 
A1 = rand(3); 
A1(end,1) = A1(1); 
A2 = rand(3); 
% check identical elements 
[~,~,ic] = unique(A1(:,1),'stable'); 
identicalNumbers = any(histcounts(ic,max(ic)) > 1) % true 
[~,~,ic] = unique(A2(:,1),'stable'); 
identicalNumbers = any(histcounts(ic,max(ic)) > 1) % false 
行列と呼ばA.でCSVファイルは、その後、次のコードを試します

編集それがさらに簡単に行うことができます。

identicalNumbers = numel(ic) > max(ic) 
0

私は自分のコードを変更しました。以下のコードは、firs列の同じ番号を検出し、インデックスを示します。

A = randi (8,6) 
    uniqueVals = unique(A(:,1)); 
    [c r] = size(uniqueVals); 
    for i=1:c 
     [m n]= size(find(A(:,1) == uniqueVals(i))); 
     if m>1 
      disp('same values detected in rows: ') 
      find(A(:,1) == uniqueVals(i)) 
     end 
    end 

コードをチェックしてフィードバックをください。

+0

私は本当にあなたが助けようとしていることに感謝していますが、おそらく私の問題が何であるか説明するのに十分ではないと思います。 私はN-by-Mテーブルを持っています。 最初の列はSXXXXXXの2番目の表示名で、残りは単一の数字です。 SXXXXXXの最初の列だけを見て、最初の列の文字列が同じかどうかを確認したい場合は、2番目の同じ番号の列が というエラーメッセージを出力する必要がありますそして、行を削除する必要があります – Ryan

+0

今、私はあなたが探しているポイントを理解しています。 CSVファイルのコピーをドロップボックスに作成すると、コード全体を書くことができます。 – Javidan

+0

あなたのcsvをファイル共有Webサイトに置き、私にリンクを送ってください。私は残りをします。 – Javidan

0

ローカルドライブにyoue csvファイルをダウンロードしました。コードを実行し、ダイアログボックスを使用してcsvファイルを選択します。

clear 
clc 
[file_name, mach_path] = uigetfile(... 
{'*.csv', 'All CSV (*.csv)'}, ... 
'Select File'); 

% If "Cancel" is selected then return 
if isequal([file_name,mach_path],[0,0]) 
    return 

% Otherwise construct the fullfilename and Check and load the file 
else 
     fileName = fullfile(mach_path,file_name); 
end 
fid = fopen(fileName,'r'); %# Open the file 
    lineArray = cell(100,1);  %# Preallocate a cell array (ideally slightly 
          %# larger than is needed) 
    lineIndex = 1;    %# Index of cell to place the next line in 
    nextLine = fgetl(fid);  %# Read the first line from the file 
    while ~isequal(nextLine,-1)   %# Loop while not at the end of the file 
    lineArray{lineIndex} = nextLine; %# Add the line to the cell array 
    lineIndex = lineIndex+1;   %# Increment the line index 
    nextLine = fgetl(fid);   %# Read the next line from the file 
    end 
    fclose(fid);     %# Close the file 
    lineArray = lineArray(1:lineIndex-1); %# Remove empty cells, if needed 
    for iLine = 1:lineIndex-1    %# Loop over lines 
    lineData = textscan(lineArray{iLine},'%s',... %# Read strings 
        'Delimiter',','); 
    lineData = lineData{1};    %# Remove cell encapsulation 
    if strcmp(lineArray{iLine}(end),',') %# Account for when the line 
     lineData{end+1} = '';      %# ends with a delimiter 
    end 
    lineArray(iLine,1:numel(lineData)) = lineData; %# Overwrite line data 
    end 
A = lineArray; 
uniqueVals = unique(A(:,1)); 
[cc ~] = size(uniqueVals); 
for i=1:cc 
[mm ~]= size(find(ismember(A(:,1),uniqueVals(i)))); 
    if mm>1 
    second = find(ismember(A(:,1),uniqueVals(i))); 
     disp('same value detected in rows: ') 
     disp(second(2)); 
    A(second(2),:) = []; 
    disp(A); 
    end 
end 
+0

このコードは、テストデータファイルを変更して同じ番号をtestdataに表示すると、実行したかったものとまったく同じですファイルはありますが、助けていただきありがとうございます。どのように動作するのか理解しようと思います。私はあなたのコメントを感謝し、問題解決のための努力をしています。 – Ryan

+0

よろしくお願いいたします。列1に同じ数字が2つ以上存在するケースを検出する場合は、「ifmm> 1」の本文のコードを変更する必要があります。 – Javidan

+0

iがデータセット全体をループし、同じ番号を検出してから変更する必要があるので、なぜsecond(i)を書くことができないのですか? – Ryan