Coud anyone help me to solve the issue.

I am having a matrix
A=[3.5204 3.7294 3.9112 4.0754 4.2294 4.3787;
0 0 0 0 0 0;
0 0 0 0 0 0;
0 0 0 0 0 0;
0.4337 0.4255 0.4162 0.4065 0.3967 0.3871]
i want to rearrange the matrix in such a way that the sum of (A,1) and sum of (A,2) should not be equal to zero.
Also the number of non zero values present in each row or column can be more than one.

3 comentarios

madhan ravi
madhan ravi el 2 de Ag. de 2019
Show how your desired matrix should look like.
A=[3.5204 0 3.9112 0 0 0;
0 0 0 4.0754 0 0.3871;
0 3.7294 0 0 0.3967 0;
0.4337 0 0.4162 0 4.2294 4.3787;
0 0.4255 0 0.4065 0 0]
with respect to this output sum(A,1) and sum(A,2) contain non zero values.
madhan ravi:
A(~sum(A,2),:)=[];
A(:,~sum(A,1))=[]
jaah navi:
I want to have the output in the following manner
A=[3.5204 0 3.9112 0 0 0;
0 0 0 4.0754 0 0.3871;
0 3.7294 0 0 0.3967 0;
0.4337 0 0.4162 0 4.2294 4.3787;
0 0.4255 0 0.4065 0 0]
madhan ravi:
Mind explaining in which logic they are rearranged??

Iniciar sesión para comentar.

 Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 2 de Ag. de 2019
Editada: Andrei Bobrov el 2 de Ag. de 2019
One variant:
[m,n] = size(A);
[~,ii] = sort(rand(m-1,n));
B = A(2:end,:);
An = [A(1,:);B(ii + (m-1)*(0:n-1))];% ATTENTION! If MATLAB < R2016b then use: An = [A(1,:);B(bsxfun(@plus,ii,(m-1)*(0:n-1)))];
jj = mod((1:m)' - (1:n),m) + 1; % for MATLAB < R2016b: jj = mod(bsxfun(@minus,(1:m)',1:n),m) + 1;
jj = jj(:,randperm(n));
out = An(sub2ind([m,n],jj,repmat(1:n,m,1)));
general case:
[m,n] = size(A);
[k,f] = max([m,n]);
p = numel(A);
V = A(randperm(p));
ii = find(V ~= 0, k, 'first');
W = [V(ii),V(setdiff(1:p,ii))];
M = reshape(W,k,[]);
if f == 1
t = n;
else
t = m;
end
MM = M(k*mod((1:t) - (1:k)',t) + (1:k)');% ATTENTION! If MATLAB < R2016b then use: MM = M(k*mod(bsxfun(@minus,1:t,(1:k)'),t) + (1:k)');
out = MM(randperm(k),:);
if f == 2
out = out';
end

8 comentarios

jaah navi
jaah navi el 2 de Ag. de 2019
It show error in line An = [A(1,:);B(ii + (m-1)*(0:n-1))]; stating Error using +
Matrix dimensions must agree.
madhan ravi
madhan ravi el 2 de Ag. de 2019
Editada: madhan ravi el 2 de Ag. de 2019
Andrei +1
@jaaj navi: Use bsxfun() instead of +
On my desktop:
>> A
A =
3.5204 3.7294 3.9112 4.0754 4.2294 4.3787
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0.4337 0.4255 0.4162 0.4065 0.3967 0.3871
>> [m,n] = size(A);
[~,ii] = sort(rand(m-1,n));
B = A(2:end,:);
An = [A(1,:);B(ii + (m-1)*(0:n-1))];
jj = mod((1:m)' - (1:n),m) + 1;
jj = jj(:,randperm(n));
out = An(sub2ind([m,n],jj,repmat(1:n,m,1)))
out =
0.4337 0 0.4162 4.0754 4.2294 0.3871
3.5204 0 0 0 0 0
0 0 0 0.4065 0.3967 4.3787
0 3.7294 0 0 0 0
0 0.4255 3.9112 0 0 0
madhan ravi
madhan ravi el 2 de Ag. de 2019
Editada: madhan ravi el 2 de Ag. de 2019
Andrei the reason jaah is receiving is because he is using version prior to 2016b , so to use implicit expansion , he needs to use bsxfun()
Andrei Bobrov
Andrei Bobrov el 2 de Ag. de 2019
Thank you Madhan!
I forgot about it again :)
madhan ravi
madhan ravi el 2 de Ag. de 2019
: ) , anyhow you're solution is brilliant!
jaah navi
jaah navi el 5 de Ag. de 2019
Thanks a lot.
jaah navi
jaah navi el 5 de Ag. de 2019
thanks a lot.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Mathematics en Centro de ayuda y File Exchange.

Preguntada:

el 2 de Ag. de 2019

Comentada:

el 5 de Ag. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by