How plot a 3-order tensor in MATLAB

1.7k Views Asked by At
a = zeros(100,100,100);
distance = [1,21,41,61,81];
for d = 1:5
    for i=distance(d): distance(d)+19
        for j=distance(d): distance(d)+19
            for k=distance(d): distance(d)+19
                a(i,j,k) = 1;
            end
        end
    end
end

The tensor a with size (100,100,100) and all element dominate the diagonal.

How to visulize a it in MATLAB with zero is white color and one is black color. I plot in MS office, this is what I want Expected image

For matrix case, we can visualize as follow

X = zeros(100,100);
distance = [1,21,41,61,81];
for d = 1:5
    for i=distance(d): distance(d)+19
        for j=distance(d): distance(d)+19
            X(i,j) = 1;
        end
    end
end


imagesc(a)
im = imagesc(1-X)
colormap(gray(256))

And the image is 2D matrix visulize

How to do a similar way for tensor?

And how to visulize tensor with noise? like noise on matrix here

2

There are 2 best solutions below

8
On BEST ANSWER

You can get pretty close to the plot produced in MS Office using isosurface and isocaps. AFAIK MATLAB doesn't have any built-in way of producing oblique projections, but if you're okay with an orthographic projection the following may work for you.

color = [0.2,0.2,0.2];
p1 = patch(isosurface(a), 'FaceColor', color, 'EdgeColor', 'none');
p2 = patch(isocaps(a), 'FaceColor', color, 'EdgeColor', 'none');

camlight left
camlight
lighting gouraud
isonormals(a, p1);

grid on;
view(3);
camorbit(-40,0);

enter image description here

0
On

I tried this with scatter3:

nonzeros = find(a);
[px,py,pz] = ind2sub(size(a),nonzeros);
scatter3(px,py,pz,'k','.');