【问题标题】:Resize a matrix in matlab and keep the same values [duplicate]在matlab中调整矩阵大小并保持相同的值[重复]
【发布时间】:2017-06-05 16:02:51
【问题描述】:

我有一个二维矩阵[n,m],其中包含分段区域的索引。我需要将 matlab 中的矩阵重塑或调整为任意大小[n',m'],而不会丢失原始值。换句话说,我需要扩展分段区域。我尝试使用 reshpae,但它不起作用,因为高度和宽度的比例必须相似。 imresize 效果不佳,因为它会更改原始值。

【问题讨论】:

  • 你的意思是像在现有矩阵中添加额外的行/列吗?
  • 我不明白你想要什么。您是否正在寻找零填充?在保持相同内容的同时扩展图像的边框?请更明确您的目标。
  • 您想使用哪些值来填充扩展区域?假设你有 10*10 的数据,你将它们扩展到 11*11,添加的 21 个值是多少?
  • imresize与插值方法nearest一起使用。

标签: matlab matrix reshape image-resizing


【解决方案1】:

这是我想出的快速方法。连接矩阵是在不影响现有数据的情况下扩展矩阵大小的一种方法:

s = zeros(3,3);
for x = 1:3 % just adds numbers so it can be studied
    s(:,x) = x;
end
t = [s, zeros(3,3)]; % adds a 3 by 3 matrix to the right
u = [s; zeros(3,3)]; % adds a 3 by 3 matrix below
v = [t; zeros(3,6)]; % adds a 3 by 6 matrix below t matrix

如果需要其他解决方案,请告诉我。这很简单,但我不明白你想要什么。

【讨论】:

  • 我认为您建议连接其他矩阵以达到预期的大小。实际上,我希望重塑矩阵并保持相同的值而不是相同的索引。
【解决方案2】:

我尝试在不使用循环的情况下解决它,如下所示:

function m1=reshapez(m,sz)

 sa = sz(1) / size(m,1);      % height scale between original matrix and desired one  
 sb = sz(2) / size(m,2);      % width scale between original matrix and desired one  
 a2 = ceil([1:sz(1)]./sa);    % corresponding indices (x) of the desired matrix in the original one
 b2 = ceil([1:sz(2)]./sb);    % corresponding indices (y) of the desired matrix in the original one
 m1 = m(a2,b2);               % desired matrix

【讨论】:

  • 使用imresizenearest 方法要容易得多。这将执行最近邻插值,这正是您在上面所做的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-08
  • 2010-10-22
  • 2012-09-13
  • 2014-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多