# Replace values on a column filtering with a value

1 view (last 30 days)
Alessandro Togni on 27 Jan 2021
Answered: Daniel Catton on 27 Jan 2021
Hi,
i have a matrix with 56 columns and want to replace with NaN values on a column N corresponding to positions on another column M containing values > of a threshold.
Let's say: if the i-th value of the column 56 is greater than threshold, replace the i-th value of the column N with NaN.
Logical indexing would be better.
Doing this i'm selecting the values greater than 8 on the columns, erasing the others.
R_mod=R(R(:, 56) > 8.0, :)
Alessandro

Mathieu NOE on 27 Jan 2021
hello
something like that :
%% some dummy data
A = (1:10);
In_Matrix =A'*A;
%% main code %%
Out_Matrix = In_Matrix;
threshold = 45;
col_index_to_test = 9; % column index where you do the test > threshold
col_index_to_replace = 8; % column indexwhere you want to replace values by NaN
ind = find(In_Matrix(:,col_index_to_test) > threshold);
Out_Matrix(ind,col_index_to_replace) = NaN;

Daniel Catton on 27 Jan 2021
I think I understand what you mean, this works for my understanding of your problem but let me know if I have misunderstood. The user will input their values of 'YourMatrix', 'M' and 'N' and the script will output your 'NewMatrix'.
YourMatrix = ;
M = ;
N = ;
[x,y] = size(YourMatrix);
NewMatrix = YourMatrix;
for i = 1:x
j = YourMatrix(i,M);
if j <= 8
NewMatrix(i,N) = NaN;
end
end