简体   繁体   中英

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,

[center #1, LU] = {0, 1, 2, 5, 10, 7, 4, 2, 1, 0} *LRUD = Left, Right, Up, Down
[center #2, RU] = {0, 1, 1, 4, 12, 7, 5, 3, 2, 1}
[center #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}
And when 5th index of heatmap, ([#1]=10, [#2]=12, [#3]=11, [#4]=11) heatmap needs to be like this image.
Heatmap image
Also can predict heatmap is all blue when 1st index ([#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)

How can I get 2D matrix from 1D arrays on Matlab? 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. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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