Borrar filtros
Borrar filtros

Change array elements that meet a condition - Multidimensional array case

2 visualizaciones (últimos 30 días)
RD
RD el 21 de En. de 2022
Comentada: Rik el 21 de En. de 2022
I can handle this in the case of two-dimensional arrays.
I have trouble in the case of multidimensional arrays. For example
A=ones(4,4,2);
B=zeros(4); B(2:4,2:4)=3;
I would like to change the value of all elements on each page of A this way
A=reshape(A,16,2);
B= reshape(B,16,1);
A(B==3,:)=0;
A=reshape(A, 4, 4, 2)
A =
A(:,:,1) = 1 1 1 1 1 0 0 0 1 0 0 0 1 0 0 0 A(:,:,2) = 1 1 1 1 1 0 0 0 1 0 0 0 1 0 0 0
Is it possible to obtain that result without reshapes?
In general, how to handle conditions that apply only to some indexes in multidimensional arrays?
PS: A and B are very large data matrices.

Respuestas (1)

Rik
Rik el 21 de En. de 2022
I can think of two ways:
  1. using a loop
  2. replicating B in the third dimension to match the size of A
I suspect the first way would be faster, but I think that might depend on the sizes of the array and the physical memory.
  2 comentarios
RD
RD el 21 de En. de 2022
Editada: RD el 21 de En. de 2022
So maybe my solution with reshape is the optimum because no for loop is required and doesn't affect physical memory. Am I right?
Rik
Rik el 21 de En. de 2022
A for loop may actually be very efficient, unless the third dimension is large relative to the product of the other two. I don't think the reshape calls are that cheap in the end.
The only way to know for sure is to test it.

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by