Question

I have big triangular matrix.

enter image description here

which has relevant data marked by different colours. I want to remove conditionally the points marked by greenish-yellowish contours:

  • Remove greedily on the area of non-linear objects
  • Remove lazily on the area of linear objects with probability 50%

My attempt

I run based on Shai's comment

% remove linear things on nonlinear area lazily: matrix(97:103, 1:98)
% remove linear things greedily elsewhere
for row=0:97
    for column=0:111
        % Lazy removal
        if and(row > 97, row < 104)
            if and(column > 0, column < 98)
                if randn > 0
                    matrix( matrix < 9 ) = 0;
                end
            end
        end

        % Greedy removal
        if or(column < 97, column > 104)
            % Remove all points in these regions because no linear objects here
            matrix(:, 1:97) = 0;
            matrix(:, 104:111) = 0;
        end
    end
end

I get

enter image description here

which is lot better than the unconditional removal

enter image description here

but still the conditional part of lazy removal can be improved. I think you cannot use here Shai's shorter version, and nested loops must be used because you have conditional removal.

You cannot use contour lines, like contour(matrix, clines) because the non-linear objects cover the linear objects too. So you need conditional removal by selecting specific area of the figure for greedy removal and lazy removal. Daniel R's command, contour(...,'ShowText','on'), does not seem to help us here, and we cannot simply remove by value. I think the following figure shows the zero points, probably singularities , because there should be 111 singularities in the figure. Does the following figure show singularities or only zero values of the data?

enter image description here

How can you apply a specific removal rule to the area of linear objects?

How can you remove conditionally the points that are marked by greenish-yellowish color in the triangular matrix in Matlab?

Was it helpful?

Solution

Your data is complex, Knowing that, and knowing what the contour function is doing with complex numbers - just plotting the real part - it is easy to filter your data.

Just set every element of your matrix to NaN, your threshold you can find out by just plotting a colorbar with your original plot and guess the values. Or have a look at the real part of your data matrix, etc.

Adjust the threshold as desired

Be aware that the colorbars should be equal for comparison

load('tri4_good.mat');
%original plot
figure(1)
[~,h] = contour(samii);
colorbar
caxis([-2,1.5].*10^7)

%get data
X = get(h,'Xdata');
Y = get(h,'Ydata');
Z = real(samii);
%this plot equals the original one
figure(2)
contour(X,Y,Z)
colorbar
caxis([-2,1.5].*10^7)

%therefore the Z-Data is equal to the real part of your data
%X and Y are the indices of your data matrix
%set the threshold as desired
thresh = 0.1*10^7;

idx = find( abs(Z) < thresh);  %or what condition you like to use.
Z(idx) = NaN;

%filtered plot
figure(3)
contour(X,Y,Z)
colorbar
caxis([-2,1.5].*10^7)

gives you:

enter image description here


regarding all your comments, these lines should be the right way to go:

Z = abs(samii);
idx = find(Z == 0);
Z(idx) = NaN;

%filtered plot
figure(1)
contour(Z);
colorbar
caxis([-2,1.5].*10^7)

returns:

enter image description here

If this doesn't fit your needs, you have to completely overthink everything ;)

OTHER TIPS

A simple way, you can just change the value of a matrix element, when it is not equal than VALUE change it 0. Ask a green/yellow color number VALUE in the color scheme, say it is VALUE=125.

for i=1:size(matrix,1)
   for j=1:size(matrix,2)
     if(matrix(i,j)==125)
        matrix(i,j) = 0;
   end;
end;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top