Can someone solve my problem?

1 Ansicht (letzte 30 Tage)
Majid
Majid am 3 Okt. 2022
Bearbeitet: Majid am 4 Okt. 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 Kommentare
Image Analyst
Image Analyst am 3 Okt. 2022
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 am 3 Okt. 2022
@Image Analyst how can i generate K at first?

Melden Sie sich an, um zu kommentieren.

Antworten (2)

dpb
dpb am 3 Okt. 2022
Verschoben: dpb am 3 Okt. 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.

Image Analyst
Image Analyst am 3 Okt. 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 Kommentare
Image Analyst
Image Analyst am 4 Okt. 2022
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 am 4 Okt. 2022
Bearbeitet: Majid am 4 Okt. 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Import and Export finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by