Borrar filtros
Borrar filtros

Indexing a matrix with an array of ranges

2 visualizaciones (últimos 30 días)
William
William el 2 de En. de 2013
I was wondering if it was possible to do the following:
indexArray = [4 2 0];
in = [1:10]'; in = repmat(in, 1, length(indexArray));
out = zeros(size(in));
out(1:end-indexArray,:) = in(indexArray+1:end,:);
so that:
out(:,1)' = [5 6 7 8 9 10 0 0 0 0]
out(:,2)' = [3 4 5 6 7 8 9 10 0 0]
out(:,3)' = [1 2 3 4 5 6 7 8 9 10]
The goal is to do this without being forced to replace the 4th line above with a for loop like:
for i = 1:length(indexArray)
out(1:end-indexArray(i),i) = in(indexArray(i)+1:end,i);
end
Thanks for your help in advance.

Respuestas (2)

Matt J
Matt J el 2 de En. de 2013
Editada: Matt J el 2 de En. de 2013
My recommendation from your previous related question on this was that you precompute a table of all the desired shifts:
n=length(in);
Table= fliplr(toeplitz([in(n),zeros(1,n-1)], in(n:-1:1) ));
Table(end, end+1)=0;
Then you would just do
out=Table(:,indexArray+1)
  4 comentarios
William
William el 2 de En. de 2013
So are you saying this method won't suit you? You should probably mention, then, the typical values of M=length(indexArray) and N=length(in) so that we have a feel for the data sizes involved.
True, although I was afraid of making my question too specific. N=length(in) is at least 10e6 but M=length(indexArray) will always be smaller than 100.
You never said that indexArray(i) couldn't equal N. Presumably, in that case, you would want out(:,i) to be all zeros.
True. I figured it was for that but I wasn't sure. In my case, no, indexArray will never equal N.
Matt J
Matt J el 2 de En. de 2013
Editada: Matt J el 2 de En. de 2013
N=length(in) is at least 10e6 but M=length(indexArray) will always be smaller than 100.
Sounds like a for-loop would be best, but see also my other Answer.
Is "in" of type sparse?

Iniciar sesión para comentar.


Matt J
Matt J el 2 de En. de 2013
Editada: Matt J el 2 de En. de 2013
Another method, which may be preferable if M=length(indexArray) is short.
M=length(indexArray);
N=length(in);
e=(1:N).';
Iout=bsxfun(@gt,e,indexArray);
Iin=bsxfun(@times,e,Iout);
out=zeros(N,M);
out(flipud(Iout))=in(nonzeros(Iin)),

Categorías

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

Community Treasure Hunt

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

Start Hunting!

Translated by