change equal values in a column to nan
3 ビュー (過去 30 日間)
古いコメントを表示
I have matrix with several columns and rows. Most of the columns are like (1.02 1.05 1.03 1.04 1.04 1.04 1.04 .. 1.04). No I would like to search in my matrix in each column the point from where the following numbers in the column are equal change the second until the last value to nan.
As an example I would like to change (1.02 1.05 1.03 1.04 1.04 1.04 1.04 .. 1.04) to (1.02 1.05 1.03 1.04 nan nan nan ... nan).
But there are some columns that don't have a point from where the column has the same value until the end.
Thank you for your help.
0 件のコメント
採用された回答
John Chilleri
2017 年 2 月 6 日
編集済み: John Chilleri
2017 年 2 月 8 日
Hello,
Try this:
% Given matrix A
for i = 1:size(A,2) % go through each column
for j = 2:size(A,1) % through elements in column
if (sum(A(j-1,i) == A(j:end,i)) == size(A,1)-j+1)
A(j:end,i) = NaN;
break;
end
end
end
If you have any questions please ask! This will only replace the values if they're forever equivalent (if it repeats 2,2,2,3, it wont replace the 2,2,2).
Hope this helps!
3 件のコメント
Jan
2017 年 2 月 8 日
The comparison
if (sum(A(j-1,i) == A(j:end,i)) == length(A(j:end,i)))
is more expensive than required. The length of A(j:end,i) is size(A,1)-j+1. The explicit creation of A(j:end,i) only to determine its length, consumes time.
A simplification:
for j = size(A,1)-1:1 % through elements in column
if A(j,i) == A(j+1,i)
A(j+1,i) = NaN;
else
break;
end
end
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で NaNs についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!