How do I create a heatmap filled with NaN values for preset value of rows and columns?

13 次查看(过去 30 天)
I am making multiple heat maps with varying amounts of data. I want to create a blank heatmap of NaN values so all of my maps are 30x15, even if there isn't that much data in each.
How should I go about doing this?

采纳的回答

dpb
dpb 2025-3-25
编辑:dpb 2025-3-25
HM=heatmap(nan(30,15));
You don't need such; just fill in an array of that size with the actual data at some point chosen for the upper RH corner for the real data to reside.
HR=30; HC=15;
A=nan(HR,HC);
R=20; C=10;
r=floor((HR-R)/2);
c=floor((HC-C)/2);
M=randi(200,[R,C]);
A(r:r+R-1,c:c+C-1)=M;
heatmap(A)
  3 个评论
Steven Lord
Steven Lord 2025-3-25
If you want the data to be centered and you're using release R2023b or later, you can use the paddata function.
A = randi(10, 12, 17);
P1 = paddata(A, [30 28], FillValue = NaN);
P2 = paddata(A, [30 28], FillValue = NaN, Side = "both");
figure
heatmap(A)
figure
heatmap(P1, Title="A is in upper-left corner")
figure
heatmap(P2, Title="A is centered")
dpb
dpb 2025-3-26
P1 = paddata(A, [30 28], FillValue = NaN);
P2 = paddata(A, [30 28], FillValue = NaN, Side = "both");
When was the NamedParameterName=Value syntax introduced Steven? That surely confused an old fogey like me for a while until I figured it must be equivalent to and tried
P1 = paddata(A, [30 28], 'FillValue',NaN);
P2 = paddata(A, [30 28], 'FillValue',NaN, 'Side',"both");
to be certain. :)
As we've noted before, I'm still on R2021b for IT conflict-avoidance issues...

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Distribution Plots 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by