2017-04-09 136 views
0

我有一个csv文件,它由N-M表组成。 在第一式柱的每一行包括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

首先读出的在矩阵叫做A. csv文件,然后尝试下面的代码:的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

我不能得到这个工作。 它不会在colum中检测到相同的值,然后将其删除并显示错误消息 – 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

使用第三输出和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 

编辑这是可以做到更简单:

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

我已经修改我的代码。下面的代码检测第一列中的相同数字,并告诉您索引:

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-M表。 第一个柱是SXXXXXX第二个显示名称,其余是单个数字。 我想只看看第一列的SXXXXXX,并检查第一列中的任何字符串是否相同,如果是这种情况,它应该打印一条错误消息,其中第二个相同的数字出现在哪一行 然后行应该被删除 – Ryan

+0

现在,我明白你要找的点。如果您在保存箱中制作csv文件的副本,则可以为您编写整个代码。 – Javidan

+0

将您的csv放入文件共享网站并向我发送链接。我会做剩下的。 – 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

如果我更改测试数据文件并使相同的数字在测试数据中出现更多,此代码完全符合我希望它执行的任务文件,但感谢您的帮助我会尝试了解它是如何工作的我欣赏您的所有评论,并努力帮助我解决问题 – Ryan

+0

欢迎您。如果要检测第1列中相同数字不止两次出现的情况,则需要修改“if mm> 1”主体中的代码。 – Javidan

+0

为什么不可能写第二个(i),因为我遍历整个数据集并检测到任何相同的数字,然后改变它? – Ryan