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

1 回表示 (過去 30 日間)
yue ishida
yue ishida 2017 年 7 月 5 日
回答済み: Andrei Bobrov 2017 年 7 月 5 日
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 [] [] [] []

採用された回答

Andrei Bobrov
Andrei Bobrov 2017 年 7 月 5 日
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

その他の回答 (2 件)

Guillaume
Guillaume 2017 年 7 月 5 日
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 2017 年 7 月 5 日
編集済み: José-Luis 2017 年 7 月 5 日
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;

製品

Community Treasure Hunt

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

Start Hunting!

Translated by