繁体   English   中英

如何在MATLAB上将一维数组转换为二维矩阵?

[英]How can I convert 1D array into 2D matrix on MATLAB?

我想用一维阵列制作热图,这是我的计划;
假设中心有4个点,每个点都有一个数组,

[中心#1,LU] = {0,1,2,5,10,7,4,2,1,0} * LRUD =左,右,上,下
[中心#2,RU] = {0,1,1,4,12,12,7,5,3,2,1}
[中心#3,LD] = {0,1,3,4,11,7,4,2,1,0}
[中心#4,RD] = {0,1,3,6,11,6,6,5,3,1,1}
当热图的第5个索引时,[[#1] = 10,[#2] = 12,[#3] = 11,[#4] = 11)热图需要类似于此图像。
热图图像
当第一个索引([#1] = 0,[#2] = 0,[#3] = 0,[#4] = 0)时,也可以预测热图全是蓝色
最后一个索引时,只有右侧的颜色几乎是蓝色。 ([#1] = 0,[#2] = 1,[#3] = 0,[#4] = 1)

如何在Matlab上从1D数组中获得2D矩阵? 从中心递减的值可以是线性的,也可以是其他值。

根据您的示例,您希望始终生成4 n * n个矩阵,其中每个矩阵的中心点获取数组中的值,而其所有4个邻居的值均递减,直到零。 我说对了吗?

这是否会创建您要创建的四个矩阵之一? 如果是这样,只需修改参数并制作四个矩阵并将它们绘制在一起

% your matrix size
size = 15

center =  (size + 1) / 2

center_value = 5

mat_a = zeros(size,size);
mat_a(center,center) = center_value;
%loop all values until zero
for ii=1:center_value -1
  current_value = center_value - ii;
  update_mat = mat_a;
  % loop over matrix, check if 4-neighbors non-zero
  for x =1:size
    for y =1:size
      if ( mat_a(y,x) == 0 )
        has_non_zero_neighbor = false;
        % case 1 
        if ( x < size) 
          if (mat_a(y,x+1) > 0)
             has_non_zero_neighbor = true; 
           endif
         endif
         % case 2
         if ( y < size) 
           if (mat_a(y+1,x) > 0)
             has_non_zero_neighbor = true; 
           endif
         endif
         %case 3
         if ( x > 1)
          if (mat_a(y,x-1) > 0)
             has_non_zero_neighbor = true; 
           endif
         endif
         % case 4
         if ( y > 1) 
          if (mat_a(y-1,x) > 0)
             has_non_zero_neighbor = true; 
           endif
         endif  

         %if non-zeros, update matrix item value to current value
         if (has_non_zero_neighbor == true)    
           update_mat(y,x) = current_value;
         endif 
      endif

    end
  end
  mat_a = update_mat;

end

figure(1)
imshow(mat_a./center_value)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM