簡體   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