How can already used elements be eliminated one by one in 2D matrix while moving upwards? | Efficienlty

 Respuesta aceptada

r = rot90(inputMatrix,-1); % turn rows into columns with last row as first column
[~,ix] = unique(r); % get the unique linear indexing with preference to leftmost columns
result = zeros(size(r)); % initialize result as all 0's
result(ix) = r(ix); % fill in the unique values with preference to leftmost columns
result = rot90(result); % rotate back to original orientation

Más respuestas (1)

Use unique():
inputMatrix = [1 2 0;
2 3 4;
4 5 6]
[~, ia, ic] = unique(inputMatrix);
output = zeros(size(inputMatrix)); % Initialize
output(ia) = inputMatrix(ia)

4 comentarios

Thanks for response. But it gives undesired results for some inputs.
e.g.
inputMatrix = [1 2 0;
4 3 1;
4 5 6]
output =
1 2 0
4 3 0
0 5 6
outputShouldBe =
2 0 0
3 1 0
4 5 6
So it appears you have added an additional requirement, that of moving elements to the left. Are there any other requirements that you haven't told us about?
That output contradicts the original pattern. For that "should be" output to be true, then the output for
input = [1 2 0;
2 3 4;
4 5 6];
should be
[1 2 0;
3 4 0;
5 6 0;]
@James Tursa
"Moving elements to the left" is not a mandatory requirement.
inputMatrix = [1 2 0;
4 3 1;
4 5 6]
outputCouldBe =
0 2 0
0 3 1
4 5 6
Above output matrix also holds good :)
At the end, more priority given to bottom rows is what required.
I mean, the last row's elements are unchanged. 2nd last row's common elements turn to zero. 3rd last row's common elements(with 2nd last and very last rows) turn to zero.

Iniciar sesión para comentar.

Categorías

Productos

Versión

R2018b

Etiquetas

Preguntada:

el 2 de Nov. de 2018

Comentada:

el 3 de Nov. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by