could anyone help me to solve the following issue
1 view (last 30 days)
Show older comments
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 Comments
Accepted Answer
Image Analyst
on 6 Jun 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
0 Comments
More Answers (1)
PIYUSH AGGARWAL
on 3 Jun 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.
3 Comments
PIYUSH AGGARWAL
on 5 Jun 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 :) )
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!