필터 지우기
필터 지우기

could anyone help me to solve the following issue

조회 수: 3 (최근 30일)
jaah navi
jaah navi 2019년 6월 3일
답변: Image Analyst 2019년 6월 6일
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
TADA
TADA 2019년 6월 3일
Do you want it to happen randomly?
jaah navi
jaah navi 2019년 6월 3일
yes randomly.

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2019년 6월 6일
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

추가 답변 (1개)

PIYUSH AGGARWAL
PIYUSH AGGARWAL 2019년 6월 3일
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
PIYUSH AGGARWAL
PIYUSH AGGARWAL 2019년 6월 5일
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 2019년 6월 6일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by