Borrar filtros
Borrar filtros

Constraints applied to circshift function

2 visualizaciones (últimos 30 días)
laurie
laurie el 21 de Feb. de 2015
Respondida: Image Analyst el 21 de Feb. de 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!

Respuesta aceptada

Image Analyst
Image Analyst el 21 de Feb. de 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;

Más respuestas (0)

Categorías

Más información sobre Operating on Diagonal Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by