Constraints applied to circshift function
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
laurie
am 21 Feb. 2015
Beantwortet: Image Analyst
am 21 Feb. 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!
0 Kommentare
Akzeptierte Antwort
Image Analyst
am 21 Feb. 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;
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Whos 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!