Can someone solve my problem?

Hi all!
I have a binary matrix N(m*n) in each row i have only one "1".
Now I will check elements of N.
if N(i,j) ==1, I put "1" in the next 6 columns.
For example
N= [0 1 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 1 0 0]
the result is the matrix k, for example the last row of N, there are not enough columns so i put Nan in K.
K =[0 1 1 1 1 1 1 1 0 0 0 0
0 0 1 1 1 1 1 1 1 0 0 0
0 0 0 1 1 1 1 1 1 1 0 0
0 0 0 0 0 0 0 0 0 0 0 0]
After this i will check previous rows and generate new vector:
If there is intersection of "1" in the same columns I will do this for example :
The first row(matrix k), there is no previous row so I put "0"
The second row, there is "1" in the previous row before intersection, so I put (6-1) ="5"
The third row, there are two "1"s in the first row and one "1" in the second row, so i put (6-2)+(6-1)="9"
The last is "0", the new vector will be :
v=[0
5
9
Nan]
What I mean by the intersection is the number of "1s" existing simultaneously in the same column.
I'm wondering how to solve this. That's why I'm asking you if it is possible to do or not?

6 件のコメント

Image Analyst
Image Analyst 2022 年 10 月 3 日
It's still not clear. What exactly does "What I mean by the intersection is the number of "1s" existing simultaneously in the same column." mean? The "number of 1s..." is what??? What is the number? Zero, one, two, any number greater than 0? Where in the column? Anywhere? Just above the current row?
Majid
Majid 2022 年 10 月 3 日
編集済み: Majid 2022 年 10 月 3 日
@Image Analyst to make it simple , i just look for the first "1" in each row. After this i check the number of "1" in previous columns. i will consider all previous columns.
Image Analyst
Image Analyst 2022 年 10 月 3 日
So for matrix K:
K =[0 1 1 1 1 1 1 1 0 0 0 0
0 0 1 1 1 1 1 1 1 0 0 0
0 0 0 1 1 1 1 1 1 1 0 0
0 0 0 0 0 0 0 0 0 0 0 0]
You originally say: The third row, there are two "1"s in the first row and one "1" in the second row, so i put (6-2)+(6-1)="9"
I presume you mean "The third row of v", right?
but there are not two 1s in the first row of K, there are 7. In the second row of K there are also 7 1s, not one 1. Even if we look at N, there is always only ever one 1 in any row of N, so you can't be referring to N instead of K.
Majid
Majid 2022 年 10 月 3 日
@Image Analyst in matrix K, i talk only about the "1" before the specific element.
for the third row , i focus on the first "1" in this row, after this i verify how many "1" before it in previous rows, i return to the second is just only '1' (row 2,column 3) , i return to the first row i found two"1"(row1,column2 and row1,column 3).
i will not consider the other "1" , just for the first in each row and how many in the previous rows
Image Analyst
Image Analyst 2022 年 10 月 3 日
To find out how many 1s there are in each row (inclusive) and all prior rows:
K =[0 1 1 1 1 1 1 1 0 0 0 0
0 0 1 1 1 1 1 1 1 0 0 0
0 0 0 1 1 1 1 1 1 1 0 0
0 0 0 0 0 0 0 0 0 0 0 0];
num1s = cumsum(any(K, 2))
num1s = 4×1
1 2 3 3
Majid
Majid 2022 年 10 月 3 日
@Image Analyst how can i generate K at first?

サインインしてコメントする。

回答 (2 件)

dpb
dpb 2022 年 10 月 3 日
移動済み: dpb 2022 年 10 月 3 日

0 投票

"how can i generate K at first?"
N= [0 1 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 1 0 0];
NK=6; % number 1's to write for K
V=ones(1,NK); % the vector to write
[R,C]=size(N); % array size
K=zeros(R,C); % initialize output array
[r,c]=find(N); % locate 1's in N
for i=r.' % for each row w/ a 1
if c(i)<=(C-NK) % room enough for augmenting
K(i,c(i):c(i)+NK-1)=V;
else
K(i,c(i))=nan;
end
end
K
K = 4×12
0 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 NaN 0 0
There could be many "variaions on a theme" of the above regarding the incomplete row; we have no klew as to what the end purpose of this exercise might be so not possible to conjecture on what would be the best option. The first thought would be to simply fill the remainder of the row with 1 similar to the rest but maybe then again there is a reason to have a complete set or nothing.

1 件のコメント

Majid
Majid 2022 年 10 月 4 日
@dpb thank you for your answer!

サインインしてコメントする。

Image Analyst
Image Analyst 2022 年 10 月 3 日

0 投票

To generate K from N:
N= [0 1 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 1 0 0]
N = 4×12
0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
[rows, columns] = size(N)
rows = 4
columns = 12
K = N; % Initialize
for row = 1 : rows
% Check this row for the first 1.
thisRow = N(row, :);
firstColumn = find(thisRow, 1, 'first')
% If there is a 1...
if ~isempty(firstColumn) && firstColumn ~= columns
% Fill out 6 more columns with 1s.
lastColumn = min([columns, firstColumn + 6])
K(row, (firstColumn + 1) : lastColumn) = 1;
end
end
firstColumn = 2
lastColumn = 8
firstColumn = 3
lastColumn = 9
firstColumn = 4
lastColumn = 10
firstColumn = 10
lastColumn = 12
K
K = 4×12
0 1 1 1 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 1 1 1

4 件のコメント

Majid
Majid 2022 年 10 月 3 日
@Image Analyst it works but it miss the condition if the position of first element < 6 columns i put "nan"
dpb
dpb 2022 年 10 月 3 日
See my solution above that does just that...
Image Analyst
Image Analyst 2022 年 10 月 4 日
The first 3 rows of N all have their first 1 in a column less than 6. They occur in columns 2, 3, and 4 respectively. Yet nowhere in the N that you showed are there nans anywhere. Not in the first 3 rows, nor in the last row.
But if you now want a nan in the location of the first 1 where there is not enough room for the last column to be within the array, then you can do this (boy, this is a quirky request).
N= [0 1 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 1 0 0];
[rows, columns] = size(N);
K = N; % Initialize
for row = 1 : rows
% Check this row for the first 1.
thisRow = N(row, :);
firstColumn = find(thisRow, 1, 'first');
% If there is a 1...
if ~isempty(firstColumn)
% Fill out 6 more columns with 1s, unless they won't fit. Use nan in that case.
lastColumn = firstColumn + 6;
if lastColumn >= columns
K(row, firstColumn) = nan;
else
K(row, (firstColumn + 1) : lastColumn) = 1;
end
end
end
K
K = 4×12
0 1 1 1 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 NaN 0 0
Since this is a highly unusual thing to want to do, can you give us the larger context? Why do you want to do this quirky thing and create an array like this? There might be some other way to do it, like with a morphological operation or something.
Majid
Majid 2022 年 10 月 4 日
編集済み: Majid 2022 年 10 月 4 日
@Image Analyst the matrix N is a matrix of starting a such task, and each task should take in process 6 seconds (the number of columns is the time) so I put "Nan" because I don't have enough time to do it. for example in the last row the task is starting in 10th second (column) and the time stop after 2 second so i reject this task because I can't satisfy.
@Image Analyst @dpb this is why in the other process i want to know if there is a task in process the other should wait. the process i mentioned above is the waiting time.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

質問済み:

2022 年 10 月 3 日

編集済み:

2022 年 10 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by