Borrar filtros
Borrar filtros

How to Subtract a matrix from another matrix of different size?

6 visualizaciones (últimos 30 días)
a = [2 3; 8 9; 88 90; 4 8]; b = [8 9]; I want c = [2 3; 88 90; 4 8];
1. I tried a-b, but that gives me error due to different size of matrix.
2. If I apply a(b) =[]; that would give me result, but it will be inefficient when b is larger in size.
  1 comentario
Stephen23
Stephen23 el 28 de Mayo de 2015
Editada: Stephen23 el 28 de Mayo de 2015
"it will be inefficient when b is larger in size"... actually the concept itself is inefficient. Any method that you find that has to remove some elements from a large array and then reallocate that array to new contiguous memory is going to be inefficient. It is the changing of arrays that is inefficient, so the solution is don't change arrays (unless you really need to).
It is likely that the fastest and most efficient solution would be to keep the data array unchanged, and simply use logical indexing to refer to the array locations that you need to work with.

Iniciar sesión para comentar.

Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 28 de Mayo de 2015
setdiff(a,b,'rows')

Más respuestas (2)

Image Analyst
Image Analyst el 28 de Mayo de 2015
WHY do you want to do this? It would help to know what your ultimate goal is. For example, maybe you could just resize one of the arrays so that they ARE the same size.

Salaheddin Hosseinzadeh
Salaheddin Hosseinzadeh el 28 de Mayo de 2015
Hi Tanmoy,
You are almost close
First check which row of the matrix a is equal to b for that you can use isequal
you can check one at a time
for i = 1:size(a,1)
if ~isequal(a(i,:),b)
c(i,:) = [a(i,:)];
end
end
something like this will solve your problem, however you may find some more efficient way.
Good Luck!
  1 comentario
Salaheddin Hosseinzadeh
Salaheddin Hosseinzadeh el 28 de Mayo de 2015
Editada: Salaheddin Hosseinzadeh el 28 de Mayo de 2015
You may need to predefine c as with the same size as a.
Another way is to take all the i values for which two rows are not equal and assign all those rows to a c at once.
There are several ways to do it, and thats just personal preference.
One thing for sure is that you DO NOT WANT to SUBTRACT!! It's not subtraction you're looking for.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by