Borrar filtros
Borrar filtros

could anyone help me to solve the following issue

3 visualizaciones (últimos 30 días)
jaah navi
jaah navi el 3 de Jun. de 2019
Respondida: Image Analyst el 6 de Jun. de 2019
A=[4.7908 5.0104 5.1329 5.3186 5.3094 5.3070 5.4452 5.5001;
0.0958 0.0994 0 0 0.1848 0.2020 0.1851 0.2146;
0.0848 0 0.1469 0.1859 0.1767 0.1854 0.1856 0.1648;
0 0 0 0 0 0 0 0;
0 0.0896 0.1619 0.1351 0 0 0 0;
0 0 0 0 0 0 0 0]
In row 4 and row 6 all numbers are found to be zero.
Could anyone help me how to rearrange the existing numbers in each column such that each row should contain at least one non zero value and each column should containg three non zero values.
  2 comentarios
TADA
TADA el 3 de Jun. de 2019
Do you want it to happen randomly?
jaah navi
jaah navi el 3 de Jun. de 2019
yes randomly.

Iniciar sesión para comentar.

Respuesta aceptada

Image Analyst
Image Analyst el 6 de Jun. de 2019
Here is one way. Just brute force scramble the array until it meets the criteria:
A=[4.7908 5.0104 5.1329 5.3186 5.3094 5.3070 5.4452 5.5001;
0.0958 0.0994 0 0 0.1848 0.2020 0.1851 0.2146;
0.0848 0 0.1469 0.1859 0.1767 0.1854 0.1856 0.1648;
0 0 0 0 0 0 0 0;
0 0.0896 0.1619 0.1351 0 0 0 0;
0 0 0 0 0 0 0 0];
[rows, columns] = size(A)
tryAgain = true;
maxIterations = 1000000;
loopCounter = 0;
while tryAgain && loopCounter < maxIterations
randomIndexes = randperm(numel(A));
A = reshape(A(randomIndexes), rows, columns);
% Check that each row has at least 1 non-zero value
rowsOK = sum(any(A, 2)) == rows;
% Check that each column has at least 3 non-zero values
columnsOK = all(sum(A ~= 0, 1) >= 3);
loopCounter = loopCounter + 1;
if rowsOK && columnsOK
break
end
end
fprintf('Found this A after %d iterations:\n', loopCounter);
A

Más respuestas (1)

PIYUSH AGGARWAL
PIYUSH AGGARWAL el 3 de Jun. de 2019
Hi Jaah,
I just switched the values marked in the figure in MATLAB command line.
I think now your requirement of having atleast one non zero number in each row and atleast three non zero numbers in a column is satisfied.
Hope that this works for you
Feel free to reply to this thread if you have additional questions. Furthermore, if this did not address the question you asked, please let me know what I misunderstood so that I can ivestigate further.​
Capture.JPG
  3 comentarios
PIYUSH AGGARWAL
PIYUSH AGGARWAL el 5 de Jun. de 2019
I can help you provide the code if you tell me what exactly what you want to do with this data because you said something about randomness in the baove comments. Please explain a little further about the randomess comment there, Are you generating this data using rand functions ? And pls upvote answers (will be a help for us :) )
jaah navi
jaah navi el 6 de Jun. de 2019
What I need exactly is
sum(A~=0,2) and
sum(A~=0,1) should not be equal to zero and it can equal to 1 or more than 1.
Could you please help me on it.

Iniciar sesión para comentar.

Categorías

Más información sobre Programming 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