简体   繁体   English

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

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

I want to make heatmap with 1D array(s), this is my plan; 我想用一维阵列制作热图,这是我的计划;
Let assume 4 points of center and each has array, 假设中心有4个点,每个点都有一个数组,

[center #1, LU] = {0, 1, 2, 5, 10, 7, 4, 2, 1, 0} *LRUD = Left, Right, Up, Down [中心#1,LU] = {0,1,2,5,10,7,4,2,1,0} * LRUD =左,右,上,下
[center #2, RU] = {0, 1, 1, 4, 12, 7, 5, 3, 2, 1} [中心#2,RU] = {0,1,1,4,12,12,7,5,3,2,1}
[center #3, LD] = {0, 1, 3, 4, 11, 7, 4, 2, 1, 0} [中心#3,LD] = {0,1,3,4,11,7,4,2,1,0}
[center #4, RD] = {0, 1, 3, 6, 11, 6, 5, 3, 1, 1} [中心#4,RD] = {0,1,3,6,11,6,6,5,3,1,1}
And when 5th index of heatmap, ([#1]=10, [#2]=12, [#3]=11, [#4]=11) heatmap needs to be like this image. 当热图的第5个索引时,[[#1] = 10,[#2] = 12,[#3] = 11,[#4] = 11)热图需要类似于此图像。
Heatmap image 热图图像
Also can predict heatmap is all blue when 1st index ([#1]=0, [#2]=0, [#3]=0, [#4]=0) 当第一个索引([#1] = 0,[#2] = 0,[#3] = 0,[#4] = 0)时,也可以预测热图全是蓝色
and only right side has color that almost blue when last index. 最后一个索引时,只有右侧的颜色几乎是蓝色。 ([#1]=0, [#2]=1, [#3]=0, [#4]=1) ([#1] = 0,[#2] = 1,[#3] = 0,[#4] = 1)

How can I get 2D matrix from 1D arrays on Matlab? 如何在Matlab上从1D数组中获得2D矩阵? Decreasing values from center can be linear or whatever. 从中心递减的值可以是线性的,也可以是其他值。

Based on your example you wish to produce always 4 n * n matrices, where the center point of each matrix gets the value in your arrays and all its 4-neighbors get a decreasing value until zero. 根据您的示例,您希望始终生成4 n * n个矩阵,其中每个矩阵的中心点获取数组中的值,而其所有4个邻居的值均递减,直到零。 Did I get it right? 我说对了吗?

Does this create one of the four matrices you wished to create? 这是否会创建您要创建的四个矩阵之一? If so, just modify the parameters and make four matrices and draw them together 如果是这样,只需修改参数并制作四个矩阵并将它们绘制在一起

% 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