How to swap matrix ?? Anyone can help me to improve my code.?? I want 12No of 30's & 8 Nos of 0's in every iteration.
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
M=[30 30 30 30 30 30 0 0 0 0 0 0 0 0 30 30 30 30 30 30];
p = M(:,:,1);
Np = length(M);
for p = Np/2: -1:2
for CP = p-1:-1:1
if M(1,CP) ~= M(1,CP+1)
M(1,CP) = M(1,CP+1)
end
end
end
%I want to swap looks like
%[30 30 30 30 30 30 30 30 30 30 30 30 0 0 0 0 0 0 0 0]
%"linalg::swapCol" command only works in MuPad
1 comentario
Guillaume
el 20 de En. de 2016
Please, use the body (rather than the title) to actually ask your question.
It's not clear at all what you're asking. You've posted some code, half of which is commented out and would do something useful it wasn't, and the other half of which does something useless (just copy the middle element to all elements before). And in the end, you show a desired output which just appears to be the input sorted in descending order.
Respuestas (1)
Guillaume
el 20 de En. de 2016
If you're asking for a better implementation of your commented out code above:
M = [30 30 30 30 30 30 0 0 0 0 0 0 0 0 30 30 30 30 30 30];
M2 = dec2bin(0 : pow2(numel(M)) - 1) - '0';
M2 = M2(sum(M2, 2) == nnz(M), :);
values = unique(M);
out = values(M2 + 1)
%check that it generated the right number of permutations:
assert(size(out, 1) == factorial(numel(M)) / (factorial(nnz(M)) * factorial(numel(M) - nnz(M))))
3 comentarios
Guillaume
el 20 de En. de 2016
That code makes no sense to me, nor do the explanation
p = M(:, :, 1);
That doesn't make much sense when M is a row vector. It is equivalent to:
p = M;
Similarly,
Np = length(M)
only makes sense if M is a row vector.
The rest of the code (the dual loop) is equivalent to:
M(1:floor(Np/2) = M(floor(Np/2));
and most likely not what you want to do.
Ver también
Categorías
Más información sobre Matrix Indexing 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!