Info

この質問は閉じられています。 編集または回答するには再度開いてください。

half the first NaN value after the last non NaN value in each column

1 回表示 (過去 30 日間)
Qian cao
Qian cao 2015 年 11 月 30 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
Hi, I have a matrix
p = [NaN 2 NaN;
2 4 NaN;
2 6 8;
NaN 8 2;
NaN NaN 1;
NaN NaN 1]
The realized matrix
p1 = [NaN 2 NaN;
2 4 NaN;
2 6 8;
1(half of 2) 8 2;
NaN 4(half of 8) 1;
NaN NaN 1]
... Notice: Only the first NaN after the last non NaN value in each column is halved with the last non NaN value.. while the other NaN stay as they were.. Thank you for your answers. BR

回答 (2 件)

Jan
Jan 2015 年 11 月 30 日
編集済み: Jan 2015 年 11 月 30 日
p = [NaN 2 NaN;
2 4 NaN;
2 6 8;
NaN 8 2;
NaN NaN 1;
NaN NaN 1];
m = isnan(p);
for col = 1:size(p, 2)
index = strfind(m(:, k).', [false, true]); % [EDITED]
if ~isempty(index)
p(index(1) + 1, k) = p(index(1), k) / 2;
end
end
  2 件のコメント
Qian cao
Qian cao 2015 年 11 月 30 日
sorry ... it does not work out
Jan
Jan 2015 年 11 月 30 日
Please, Qian cao, giv us any chance to post an improvement. Tell us, what goes wrong. Do you get an error message or does the result differ from your expectations? I see a little typo, which is fixed now. So try it again.

Thorsten
Thorsten 2015 年 11 月 30 日
ind = diff(isnan([p; p(end,:)])) == 1;
p(ind) = p(ind)/2;
  2 件のコメント
Qian cao
Qian cao 2015 年 11 月 30 日
No...it should be the cell after p(ind)...I tried with p(ind+1) = p(ind)/2; But it does not work...Could you help me ? In your result, the last non NaN is replaced...but I need to keep it and just fill the cell after it in the column with a value that is half of it...
Thorsten
Thorsten 2015 年 11 月 30 日
編集済み: Thorsten 2015 年 11 月 30 日
Use
ind = find(diff(isnan([p; p(end,:)])) == 1);
p(ind+1) = p(ind)/2;

Community Treasure Hunt

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

Start Hunting!

Translated by