Constraints applied to circshift function
1 次查看(过去 30 天)
显示 更早的评论
If i have a matrix A:
0 0
0 1
1 1
1 1
0 0
and I applied the following shift function (to shift rows downwards):
id=randi(6,1,size(A,2));
shift = cell2mat(arrayfun(@(x) circshift(A(:,x),[id(x) 1]),(1:numel(id)),'un',0));
How would I apply the following constraint: "The ones never get separated in their respective column" such as in the below eg. with column 2
id = 1 2
A=
0 1
0 0
0 0
1 1
1 1
Thank you for any help!
0 个评论
采纳的回答
Image Analyst
2015-2-21
You can use all() or sum() to find out which rows are all zeros. Then use find() to figure out where the last row that has a 1 in it. Then you'll know how far you can shift the rows down. If you pick a random shift, make sure that it's not more than that number or else they'll go past the end of the array.
Here's a start for you:
theSum = sum(A, 2);
lastRow = find(theSum > 0, 1, 'last');
rowsThatICanShift = size(A, 1) - lastRow;
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!