如何在 MATLAB 上将一维数组转换为二维矩阵?
How can I convert 1D array into 2D matrix on MATLAB?
我想用一维数组制作热图,这是我的计划;
假设有 4 个中心点,每个点都有数组,
[中心 #1, L U] = {0, 1, 2, 5, 10, 7, 4, 2, 1, 0} *L R U D = 左、右、上、下
[中心 #2, R U] = {0, 1, 1, 4, 12, 7, 5, 3, 2, 1}
[中心 #3, L D] = {0, 1, 3, 4, 11, 7, 4, 2, 1, 0}
[中心 #4, R D] = {0, 1, 3, 6, 11, 6, 5, 3, 1, 1}
当热图的第 5 个索引时,([#1]=10, [#2]=12, [#3]=11, [#4]=11) 热图需要像这个图像。
Heatmap image
也可以预测当第一个索引 ([#1]=0, [#2]=0, [#3]=0, [#4]=0)
时热图全是蓝色并且只有右侧有颜色最后一个索引时几乎是蓝色的。 ([#1]=0, [#2]=1, [#3]=0, [#4]=1)
如何从 Matlab 上的一维数组获取二维矩阵?从中心开始递减值可以是线性的或其他。
根据您的示例,您希望始终生成 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)
我想用一维数组制作热图,这是我的计划;
假设有 4 个中心点,每个点都有数组,
[中心 #1, L U] = {0, 1, 2, 5, 10, 7, 4, 2, 1, 0} *L R U D = 左、右、上、下
[中心 #2, R U] = {0, 1, 1, 4, 12, 7, 5, 3, 2, 1}
[中心 #3, L D] = {0, 1, 3, 4, 11, 7, 4, 2, 1, 0}
[中心 #4, R D] = {0, 1, 3, 6, 11, 6, 5, 3, 1, 1}
当热图的第 5 个索引时,([#1]=10, [#2]=12, [#3]=11, [#4]=11) 热图需要像这个图像。
Heatmap image
也可以预测当第一个索引 ([#1]=0, [#2]=0, [#3]=0, [#4]=0)
时热图全是蓝色并且只有右侧有颜色最后一个索引时几乎是蓝色的。 ([#1]=0, [#2]=1, [#3]=0, [#4]=1)
如何从 Matlab 上的一维数组获取二维矩阵?从中心开始递减值可以是线性的或其他。
根据您的示例,您希望始终生成 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)