画长方体网格中的数据分布图@Matlab

该文章提供了一段Matlab代码,用于展示三维网格中的数据分布,特别是温度分布。通过三个嵌套循环,代码逐个绘制立方体来表示每个位置的温度,使用颜色映射来可视化温度范围。为了提高效率,代码可以考虑进行循环优化,因为处理大数据集时会比较耗时。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

画图

画长方体网格中的数据分布图

Matlab代码

代码由Draw 函数draw_temperature_box 函数两部分组成。在我的项目里,数据data的size比较大,10 * 20 * 100 , 中间的3个for循环里面要计算很久。

%% Draw picture
%  Data is composed of three dimensions. Each data here represents the temperature in the rectangular grid.
%  The code here takes drawing the temperature distribution in the cube as an example.
%  If you don't have temperature data, randomly generate one. eg. data = rand(10,20,100)

function y = Draw( data )

figure;
hold on;

% Define the maximum temperature for colormap normalization
max_data = max(data(:));
min_data = min(data(:));

% Iterate through the 3D temperature array and draw each cube
for x = 1:size(data, 1)
    for y = 1:size(data, 2)
        for z = 1:size(data, 3)
            draw_temperature_box(x, y, z, data(x, y, z), max_data);
        end
    end
end

% Configure the view and axis
view(3);
axis equal;
xlabel('X');
ylabel('Y');
zlabel('Z');
title('Va Distribution in the 3D Grid');
colorbar('Ticks', linspace(0, 1, 11), 'TickLabels', linspace(min_data, max_data, 11));
function draw_temperature_box(x, y, z, temperature, max_temperature)
    % Normalize temperature to the range [0, 1]
    normalized_temperature = temperature / max_temperature;

    % Convert temperature to RGB color
    cmap = colormap('jet'); % You can choose another colormap, such as 'hot', 'cool', etc.
    color_idx = ceil(normalized_temperature * size(cmap, 1));
    color = cmap(color_idx, :);

    % Draw a cube at the specified position with the specified color
    cube_size = 1; % Adjust this value if you want to change the size of the cubes
    patch('Vertices', [x, y, z; x+cube_size, y, z; x+cube_size, y+cube_size, z; x, y+cube_size, z; ...
                      x, y, z+cube_size; x+cube_size, y, z+cube_size; x+cube_size, y+cube_size, z+cube_size; x, y+cube_size, z+cube_size], ...
          'Faces', [1, 2, 3, 4; 5, 6, 7, 8; 1, 2, 6, 5; 3, 4, 8, 7; 1, 4, 8, 5; 2, 3, 7, 6], ...
          'FaceColor', color, 'EdgeColor', 'k');
end
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值