How to find elements between zeros in matrix?

Hello, I have this matrix 3x3:
A = [3,4,6,0,2,7,10,5,0,1,0,8,9,0;
0,5,2,0,1,8,9,0,4,6,10,7,0,3;
1,0,7,2,0,4,10,8,0,3,5,0,6,9]
I want to find(specify) elements between zeros for each row, so I can apply some conditions on these elements between zeros. How can I do it with loops and also for large size matrices.

댓글 수: 1

Jos (10584)
Jos (10584) 2017년 12월 20일
  • Can there be more than 1 zeros between non-zero elements?
  • Are there always the same number of blocks of non-zero elements per row?
  • What does "apply some conditions" mean?
  • What should the output look like? Same size as A? What about the zeros in A?

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

 채택된 답변

Image Analyst
Image Analyst 2017년 12월 20일

0 개 추천

Try this:
A = [3,4,6,0,2,7,10,5,0,1,0,8,9,0; 0,5,2,0,1,8,9,0,4,6,10,7,0,3; 1,0,7,2,0,4,10,8,0,3,5,0,6,9]
[rows, columns] = size(A);
for row = 1 : rows
% Extract just this one row from A.
thisRow = A(row, :);
% Get each group of non-zeros in this row.
props = regionprops(thisRow~=0, thisRow, 'PixelValues');
numGroups = length(props); % Get how many groups of non-zeros there are.
% Examine each group for meeting "constraints"
for g = 1 : numGroups
thisGroup = props(g).PixelValues
% Now do something with this group.
% Check constraints or whatever you want to do.
end
end

댓글 수: 1

Hajem Daham
Hajem Daham 2017년 12월 21일
Thanks Image Analyst, this what I am looking for. Now can you please help me to check this two constraints for each group of non-zeros as follow:
Assum that B =1:10 and the above matrix A which I used in my problem represents the random permutation for B after adding zeros. The elements in vector B are classified into these types: a = 1:3, b = 4:6, c = 7:10.
1- the first constraint is to ensure that each group of non-zero elements between zeros has no more than two elements of a,b,c. If this case exit there should be a penalty for the row which violates this constraint.
2- Assume that each element of B has a time and the maximum time for each group is 5, so the second constraint is to ensure that for each group the max time is not violated, otherwise there should be a penalty for this row.

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

추가 답변 (2개)

Matt J
Matt J 2017년 12월 20일

1 개 추천

Do not use find(). There is no reason you need actual coordinates for what you describe, and it is just extra computation. Use logical indexing instead:
map=(A~=0);
for i=1:size(A,1)
A(i,map(i,:))=....
end

댓글 수: 3

Rik
Rik 2017년 12월 20일
While it is of course better to use logical indexing whenever you can instead of find, there are some situations in which that is not possible. If you want to fit a curve to specific indices, it may be faster to just use explicit indices, instead of working with a logical matrix.
But yes: in general, use find only if you really can't use logical indexing.
Hajem Daham
Hajem Daham 2017년 12월 20일
Hi Matt, it works, but what I want is to reach non-zeros elements without removing zeros, because the final result should contain zeros also.
Matt J
Matt J 2017년 12월 20일
Hi Matt, it works, but what I want is to reach non-zeros elements without removing zeros, because the final result should contain zeros also.
Shouldn't matter...

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

카테고리

도움말 센터File Exchange에서 Sparse Matrices에 대해 자세히 알아보기

태그

질문:

2017년 12월 20일

댓글:

2017년 12월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by