To put score value based on neighbor column position value in same row
Afficher commentaires plus anciens
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 [] [] [] []
Réponse acceptée
Plus de réponses (2)
Guillaume
le 5 Juil 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.
1 commentaire
yue ishida
le 5 Juil 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;
Catégories
En savoir plus sur Matrix Indexing dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!