To put score value based on neighbor column position value in same row

1 visualización (últimos 30 días)
Hi. I have a cell funct.
funct =
'Q7' [] 'Q12G' [] 'Q12G' []
'Q12F' [] 'Q12H' [] 'Q12H' []
'Q12G' [] 'Q12I' [] 'Q12I' []
'Q12H' [] 'Q12L' [] [] []
'Q12I' [] 'Q12M' [] [] []
'Q12L' [] [] [] [] []
'Q12M' [] [] [] [] []
I would like to know, is that possible to put value from zero and become more negative automaticly on column 2, 4, and 6 based on their respective neighbor column position. If the neighbor column in left side is empty at same row, we won't put any value in that column. The output should become like this. The value will start from 0.
'Q7' 0 'Q12G' 0 'Q12G' 0
'Q12F' -1 'Q12H' -1 'Q12H' -1
'Q12G' -2 'Q12I' -2 'Q12I' -2
'Q12H' -3 'Q12L' -3 [] []
'Q12I' -4 'Q12M' -4 [] []
'Q12L' -5 [] [] [] []
'Q12M' -6 [] [] [] []

Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 5 de Jul. de 2017
fu ={{'Q7'
'Q12F'
'Q12G'
'Q12H'
'Q12I'
'Q12L'
'Q12M'};
{'Q12G'
'Q12H'
'Q12I'
'Q12L'
'Q12M'};
{'Q12G'
'Q12H'
'Q12I'}};
n = cellfun(@numel,fu);
m = max(n);
k = numel(n);
out = cell(m,2*k);
for ii = 1:k
out(1:n(ii),[2*ii-1,2*ii]) = [fu{ii},num2cell(-(0:n(ii)-1)')];
end

Más respuestas (2)

Guillaume
Guillaume el 5 de Jul. de 2017
One way:
toinsert = num2cell(repmat(-(0:size(funct, 1)-1)', 1, size(funct, 2)/2));
toinsert(cellfun(@isempty, funct(:, 1:2:end))) = {};
funct(:, 2:2:end) = toinsert;
The above will work with funct of any size as long as the number of columns is even.

José-Luis
José-Luis el 5 de Jul. de 2017
Editada: José-Luis el 5 de Jul. de 2017
idx = cellfun(@(x) ~isempty(x), funct(:,1:2:end);
fillMat = repmat([0:-1:-(size(funct,1)+1)]',1,numel(1:2:size(funct,2))) .* idx;
fillMat = mat2cell(fillMat);
funct(:,2:2:end) = fillMat;

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by