Can someone solve my problem?

2 views (last 30 days)
Majid
Majid on 3 Oct 2022
Edited: Majid on 4 Oct 2022
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 Comments
Majid
Majid on 3 Oct 2022
@Image Analyst how can i generate K at first?

Sign in to comment.

Answers (2)

dpb
dpb on 3 Oct 2022
Moved: dpb on 3 Oct 2022
"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 Comment
Majid
Majid on 4 Oct 2022
@dpb thank you for your answer!

Sign in to comment.


Image Analyst
Image Analyst on 3 Oct 2022
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 Comments
Majid
Majid on 4 Oct 2022
Edited: Majid on 4 Oct 2022
@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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!