3
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

kevin2019
  • 147
  • 1
  • 9

2 Answers2

4

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

jodag
  • 19,885
  • 5
  • 47
  • 66
  • Cool.. the surface reflect the light, can you remove it, and make it pure black color? – kevin2019 May 28 '18 at 22:17
  • is it also work if we add noise on the tensor `a`? such as the figure with have some black point outside the diagonal? – kevin2019 May 28 '18 at 22:19
  • You can remove the 4 lines in the middle to remove lighting. – jodag May 28 '18 at 22:23
  • Tensor `a` is treated as a boolean array, if you add noise do you mean randomly flipping falses to true? Then yes black patches will be created. If you mean just adding non-zero noise to the entire array you will end up with a giant black cube. – jodag May 28 '18 at 22:24
  • Thanks, I want add noise to each element of tensor like `a(i,j,k) + 0.5*rand(1,1)` – kevin2019 May 28 '18 at 22:28
  • What do you expect the visualization to look like? If you want to show only points over a certain threshold then you should probably use `b = a > thresh` then plot `b` instead of `a`. Where `thresh` is a scalar containing the minimum value you want to show. – jodag May 28 '18 at 22:29
  • I update an example for martix case about noise (see the above figure). Can you code work for noise tensor? – kevin2019 May 28 '18 at 22:49
  • I would like to help with the noisy visualization but I don't know what you really expect the output to look like. The 2D grayscale image doesn't extend nicely to 3D. If you can find an example of what you're looking for in 3D I may be able to help. – jodag May 29 '18 at 20:10
0

I tried this with scatter3:

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