comparing rows in cell array

Allcases = readtable('227.xlsx');
caseID = Allcases{:,1};
caseX = Allcases{:,2};
caseY = Allcases{:,3};
i = 1
f = {}
while i<5
g = {caseID(i), caseX(i), caseY(i)}
if *****************:
f = [f;g];
end
i = i+1;
end
Hi, I am trying to build a cell array of unique values. In this case, I am only trying to add the row g to f if it hasn't occured before in f. Could anyone please help me complete the missing code? I am very new to Matlab and have been spending hours to try and fix this problem.

1 件のコメント

Guillaume
Guillaume 2020 年 2 月 27 日
Note that:
i = 1;
while i < 5
%... some code that doesn't change i
i = i+5;
end
is more simply written as:
for i = 1:5
%... some code that doesn't change i
end

回答 (1 件)

Fangjun Jiang
Fangjun Jiang 2020 年 2 月 27 日

0 投票

unique(Allcases,'rows') might be easier.

7 件のコメント

Jonathan
Jonathan 2020 年 2 月 27 日
Sorry didn't specify but the reason it is more complicated is because I tried to simplify the problem here as part of a bigger project. Because I've got to add other data/changing, unique won't really work.
Fangjun Jiang
Fangjun Jiang 2020 年 2 月 27 日
編集済み: Fangjun Jiang 2020 年 2 月 27 日
What is the data type of caseID, caseX and caseY? Might be helpful just providing a simplified data example.
f=[];
g=[1,2,3];
f=[f;g];
g=[1,1,2];
if ~ismember(g,f,'rows')
f=[f;g];
end
Guillaume
Guillaume 2020 年 2 月 27 日
"Sorry didn't specify but the reason it is more complicated is because I tried to simplify the problem here as part of a bigger project. Because I've got to add other data/changing, unique won't really work."
This explanation is very unclear, you need to provide a lot more details. You can't expect us to keep suggesting methods that make sense with the limited information you provide and then come back with: 'sorry, won't work'.
Jonathan
Jonathan 2020 年 2 月 29 日
Sorry guys and thank you for the help so far. I want to be a bit more clear because I really appreciate the help that you are offering. It took me some bits of time to try and think about how to work it.
So eseentially what I am doing is reading data from a table, modifying it and adding it to a cell array. The data that eventually is going to be added to the cell array comprises of essentially 5 numbers (1x5 row). However, I only want the data to be added by checking if element 3 and 5 of previous rows isn't the same as element 3 and 5 of the new row to be added. The way I'm adding this data, it's only really necessary to check the last row of the cell array against the new data to be added.
Sorry for the confusion and I was wondering whether you could please help me?
Fangjun Jiang
Fangjun Jiang 2020 年 2 月 29 日
assume it is not a floating point data equal comparison,
if f{end,3}~=g{1,3} || f{end,5}~=g{1,5}
f=[f;g];
end
Jonathan
Jonathan 2020 年 2 月 29 日
Hi, thanks for the answer.
However, I have just tried this and it is telling me that the matrix dimensions must agree. I checked the dimensions and both are 1x5 cell arrays.
Jonathan
Jonathan 2020 年 2 月 29 日
oh don't worry. Just figured it out - i was using strings, not numbers!
Thanks you so much for all your help. Now i've learnt how to index cell arrays.

この質問は閉じられています。

質問済み:

2020 年 2 月 27 日

閉鎖済み:

2021 年 8 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by