How to remove additional rows from a matrix?

4 ビュー (過去 30 日間)
Paschalis Garouniatis
Paschalis Garouniatis 2016 年 8 月 17 日
Greetings! I have 2 matrices with sizes 103x2 and 101x2. The 2 rows that I want to remove are not the last ones in the first matrix. Those matrices have been created so that the second column has the same elements for each matrix. I have ran a code which I include and checks each line of the matrices to determine whether the elements in the second column are equal or not. If they are not equal then the row from the first matrix should be deleted and then the loop should start over. After running the code I get an error message which states that index exceeds matrix dimensions. I even tried to make them equal by adding zeros and the run again the code but it didn't work. I can't figure out what am I missing. Can anyone help? Thanks in advance.
i=0;
while i<abs(length(a)-length(b))
for j=1:max(length(a),length(b))
if (a(j,2)~=b(j,2))
a(j,:)=[];
i=i+1;
continue;
end
end
end

採用された回答

Guillaume
Guillaume 2016 年 8 月 17 日
編集済み: Guillaume 2016 年 8 月 18 日
Note: you're taking a risk using length with a matrix. length is the size of the largest dimension so could be the number of rows, columns, pages, etc. depending on the matrix. It's much safer to use size with an explicit dimension, in your case size(a, 1) and size(b, 1).
I think you meant to use break instead of continue. continue means skip the rest of the body of the loop and proceed to the next iteration. Since it's the last instruction within the for loop, there's nothing left to skip, so it has absolutely no effect. break would break out of the while loop and proceed with the next iteration of the while loop.
Note that if all the values in the second column are different you don't need a loop at all:
a(~ismember(a(:, 2), b(:, 2)), :) = [];
And in any case, you don't need the inner loop:
while size(a, 1) > size(b, 1)
rowtodelete = find(a(1:size(b, 1), 2) ~= b(:, 2), 1);
if isempty(rowtodelete) %all element up to height of b match
a(size(b, 1)+1:end, :) = []; %delete remaining
else
a(rowtodelete, :) = []; %delete 1st non matching row
end
end
edit: fixed bug with 2nd algorithm.
  3 件のコメント
Guillaume
Guillaume 2016 年 8 月 19 日
Note: since all your cell arrays are just 1D, there's no need to use subscript indexing when linear indexing would work just as well and involve less typing, out{i}{2} is the same as out{i, 1}{1, 2} in your case.
Also, any time you start rewriting the same code with just different inputs, you should think to extract that into a function.
I have not really tried to understand why you have a try catch in your code nor why it is going wrong. Here is how I would implement it:
First, extract the code for shortening a matrix into a function:
function shortened = equaliseheight(taller, shorter)
while size(taller, 1) > size(shorter, 1)
rowtodelete = find(taller(1:size(shorter, 1), 2) ~= shorter(:, 2), 1); %find first row for which column 2 differs
if isempty(rowtodelete) %there wasn't one within the height of shorter
taller(size(shorter, 1)+1:end, :) = []; %therefore remove everything after in taller
else
taller(rowtodelete, :) = []; %remove differing row
end
end
shortened = taller;
end
Now for the code to process your cell array:
for row = 1 : numel(out)
switch sign(size(out{row}{1}, 1) - size(out{row}{2}, 1))
case -1 %1st matrix shorter than 2nd
out{row}{2} = equaliseheight(out{row}{2}, out{row}{1});
case 1
out{row}{1} = equaliseheight{out{row}{1}, out{row}{2});
end
end
end
Note that it may be more practical (certainly to look at it in the variable editor) to change your cell array of cell arrays into a 2d cell array:
out2 = vertcat(out{:});
, in which case replace numel(out) by size(out2, 1) and any out{row}{n} by out2{row, n}
Paschalis Garouniatis
Paschalis Garouniatis 2016 年 8 月 22 日
Guillaume thanks a lot for your time and the answer. Really helpful! I have been working on the code for some time about other issues and almost forgot to comment on your answer. Finally solved it.

サインインしてコメントする。

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 8 月 17 日
編集済み: Azzi Abdelmalek 2016 年 8 月 17 日
b(j,2) is not defined for j=max(length(a),length(b))
What are you expecting as result for this small example:
a=[4 1;5 2;6 3;7 4;9 10;1 5;8 7]
b=[3 1;5 7;8 3;4 87 ;2 10]
  4 件のコメント
Guillaume
Guillaume 2016 年 8 月 18 日
Note that with either example
a(~ismember(a(:, 2), b(:, 2)), :) = []
as per my answer is enough to do the job. It will do the job as long as the values in the 2nd column of B are unique.
Paschalis Garouniatis
Paschalis Garouniatis 2016 年 8 月 18 日
編集済み: Paschalis Garouniatis 2016 年 8 月 18 日
Thank you for your answer Azzi. At some point I encountered a problem. The size of matrix a (as written in your code) is smaller than size b. I tried to use switch and set cases for p (p>0 or p<0) but I didn't make it work. How would you suggest to overcome this obstacle? Also I want to clarify something. The last line of your code means that the rest of the rows that remain and are of no use are deleted, correct? I am asking because I haven't completed the code and it hasn't worked yet.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by