Effacer les filtres
Effacer les filtres

Replace a zeros row with one under a condition very important for me

2 vues (au cours des 30 derniers jours)
Firas Al-Kharabsheh
Firas Al-Kharabsheh le 2 Avr 2016
Commenté : Rena Berman le 20 Jan 2017
if Matrix A (M x N) like this matrix as input of function
A =[0 0 1 0 1 0 1 0 1 0 1 0 1 0 0 1
0 1 1 0 1 1 1 1 1 1 1 1 0 1 0 1
1 1 0 1 0 0 0 0 0 0 0 1 1 1 1 1
0 1 0 1 1 1 0 0 0 0 0 1 1 1 1 1
0 1 1 1 1 0 1 1 0 1 1 1 1 1 0 1
0 0 1 1 0 1 1 0 1 1 1 0 1 0 0 1
1 0 1 0 1 0 1 1 1 1 0 1 1 1 0 1
0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 1];
then the function do many things
  • First one
count number of ones in each row and in each column , For example
the first row is [0 0 1 0 1 0 1 0 1 0 1 0 1 0 0 1] then the first row in the row_matrix will be [1 1 1 1 1 1 1]
the second row is [ 0 1 1 0 1 1 1 1 1 1 1 1 0 1 0 1] then the second row in row_matrix will be [2 8 1 1]
the last row is [0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 1] then in the last row in row_matrix will be [5 1]
  • this Number in row_matrix compute by if [1 1 0 1 0 1 1] then solution will be [2 1 2]
  • And do the same thing for the Column in matrix A
  • Second one
a) compute number of ones in each row in A and put save it in vector sum_row , for example
sum_row = [ 7
12
8
9
12
9
11
13
16
6 ]
b) compute number of ones in each column in A and put the result in sum_col like this
sum_column = [ 3 7 6 6 7 6 8 6 7 7 6 7 8 7 3 9]
  • third one
a) count number of element in rows of row_matrix and put it in count_row like this example
the first row in row_matrix = [1 1 1 1 1 1 1] then the first row in count_row will be [7]
the second row in row_matrix = [2 8 1 1] then the second row in count_row will be [4]
and so on ...
b) do the same thing for the column
  • the last thing
Generate a (M x N) zeros_matrix then do this equation If N == (sum_row(k) + (count_row(k) - 1) where k is number of row in each matrix , for example
example one :
the 7th row in A matrix is [1 0 1 0 1 0 1 1 1 1 0 1 1 1 0 1]
then the 7th row in row_matrix will be [1 1 1 4 3 1]
and the 7th row in sum_row will be [11]
and the 7th row in count_row will be [6]
then we apply the equation N== (sum_row(k) + (count_row(k) - 1))
N== (sum_row(7) + (count_row(7)-1) >>> 16 == (11 + (6-1) >> then 16 ==16 (True)
example 2 : the 9th row in A matrix is [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]
then the 9th row in row_matrix will be [16]
and the 9th row in sum_row will be [16]
and the 9th row in count_row will be [1]
then when we apply the equation
N== (sum_row(9) + (count_row(9)-1) >>> 16 == (16 + (1-1) >> then 16 ==16 (True)
then in 7th and 9th row in A empty_matrix will be like this
empty_matrix = [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 1 0 1 0 1 1 1 1 0 1 1 1 0 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ]
and do the same thing for a column in this equation M==(sum_column(k) + (count_column(k) - 1)
*Note i need this function to cont my graduation project *
  4 commentaires
Rena Berman
Rena Berman le 20 Jan 2017
(Answers Dev) Restored Question.

Connectez-vous pour commenter.

Réponses (2)

dpb
dpb le 2 Avr 2016
Modifié(e) : dpb le 2 Avr 2016
On the first, I know there is a submittal on File Exchange to count runs; I think it may be from Bruno Luong...but, consider what happens if you diff a row or column and what that tells you about start/stop locations...it's not really very difficult problem.
The rest are pretty simple and looking through the doc and "Getting Started" tutorial should lead you there pretty quickly...
sum_r=sum(M,2); % sum rows
sum_c=sum(M); % sum columns
The count vector is simply length(eachRow) in the answer to Part 1.
The last is convoluted enough I don't have time to dig thru the explanation to the result but Walter's advice is good...

Roger Stafford
Roger Stafford le 2 Avr 2016
I'll just answer for your "First one" in getting "the same thing for the columns in A". Let x be a column in A.
s = cumsum(diff([0;x])==1);
t = (x==1);
a = accumarray(s(t),1);
The 'a' will be a column of the various contiguous sums. In your A example for the second column you would get a = [4;2].

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by