Constraints applied to circshift function

1 回表示 (過去 30 日間)
laurie
laurie 2015 年 2 月 21 日
回答済み: Image Analyst 2015 年 2 月 21 日
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!

採用された回答

Image Analyst
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 件)

カテゴリ

Help Center および File ExchangeOperating on Diagonal Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by