Constraints applied to circshift function

2 vues (au cours des 30 derniers jours)
laurie
laurie le 21 Fév 2015
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!

Réponse acceptée

Image Analyst
Image Analyst le 21 Fév 2015
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;

Plus de réponses (0)

Catégories

En savoir plus sur Operating on Diagonal Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by