Can someone solve my problem?
1 次查看(过去 30 天)
显示 更早的评论
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
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))
回答(2 个)
dpb
2022-10-3
移动:dpb
2022-10-3
"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
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
2022-10-3
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]
[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) && firstColumn ~= columns
% Fill out 6 more columns with 1s.
lastColumn = min([columns, firstColumn + 6])
K(row, (firstColumn + 1) : lastColumn) = 1;
end
end
K
4 个评论
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
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Entering Commands 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!