Info
La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.
How to use the index of a matrix in another one?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
EDIT
I have a matrix y(k,sum(counter))=
0 1 0 0 1 0
1 0 0 1 0 0
0 1 0 0 1 0
1 0 0 1 0 0
0 1 0 0 1 0
counter(j,1) =
[ 2
1
3 ]
I want something that says for each j in the counter(j,1) find its value, then sum the columns of y that are equal to the value in the counter.
for counter (1)= 2 , then sum the first (two) columns in y
for counter(2)= 1 , sum the following (one) column ( will stay the same basically)
for counter(3) = 3, sum the following (three) columns in y
y_new = [
1 0 1
1 0 1
1 0 1
1 0 1
1 0 1 ]
2 comentarios
Azzi Abdelmalek
el 1 de Dic. de 2013
This is not clear, what is counter? , what does mean (which is equal 2)?
Respuestas (3)
Wayne King
el 1 de Dic. de 2013
You are missing something in your description, summing the first two columns of y does not result in y_new. How did you get a third column of all ones in y_new?
idx = find(counter==1,1,'first');
B = A;
B(:,1) = sum(A(:,1:2),2);
B(:,2) = [];
The above creates a new matrix B with the first column equal to sum of the first two columns of A. But again what you described does not result in y_new
1 comentario
Azzi Abdelmalek
el 2 de Dic. de 2013
y=[ 0 1 0 1 1 0
1 0 0 1 0 0
0 1 0 0 1 0
1 0 0 1 0 0
0 1 0 0 1 0]
counter =[2;1;3];
ii=1;
idx1=1;
for k=counter'
idx2=idx1+k-1;
out(:,ii)=sum(y(:,idx1:idx2),2);
idx1=idx2+1;
ii=ii+1;
end
out
0 comentarios
Andrei Bobrov
el 2 de Dic. de 2013
Editada: Andrei Bobrov
el 2 de Dic. de 2013
iend = cumsum(counter);
ifs = iend - counter + 1;
idx = zeros(iend(end),1);
idx(ifs) = 1;
idx = cumsum(idx);
[ii,jj] = ndgrid(1:size(y,1;),idx);
out = accumarray([ii(:),jj(:)],y(:));
or
out = cell2mat(cellfun(@(x)sum(x,2),mat2cell(y,size(y,1),counter),'un',0));
or
i0 = cumsum(counter);
i1 = i0 - counter + 1;
for ii = numel(counter):-1:1
out(:,ii) = sum(y(:,i1(ii):i0(ii)),2);
end
0 comentarios
La pregunta está cerrada.
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!