I have a 2 dimentional matrix [n,m]
containing indices of segmented regions. I need to reshape or resize the matrix in matlab to any size [n',m']
without losing the original values. In other words, I need to extend the segmented regions. I tried to use reshpae, but it did not work since the scale must be similar for both the height and the width. imresize did not work as well because it changes the orignal values.
Asked
Active
Viewed 689 times
0

Yacine
- 321
- 2
- 15
-
Do you mean something like adding on additional rows/columns to the existing matrix? – Numbers682 Jun 05 '17 at 16:07
-
1I'm not understanding what it is you want. Are you perhaps looking at zero-padding? Extending the borders of the image while keeping the same content? Please be more clear on your objectives. – rayryeng Jun 05 '17 at 16:22
-
what values do you want to use to fill the extended regions? say you have 10*10 data, and you extend them to 11*11, what are the added 21 values? – Anthony Jun 05 '17 at 17:27
-
Use `imresize` with the interpolation method `nearest`. – rahnema1 Jun 05 '17 at 18:21
2 Answers
0
Here's something quick that I came up with. Concatenating matrices is one way to extend the size of a matrix without messing with existing data:
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
Let me know if another solution is needed. It's simple, but I don't understand what you want.

Numbers682
- 129
- 1
- 11
-
I think you proposed to concatenate additional matrices in order to reach the desried size. Actually, I look to reshape the matrix and keep the same values not the same indices. – Yacine Jun 05 '17 at 16:34
0
I tried to solve it without using loops as follows:
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

Yacine
- 321
- 2
- 15
-
It's much easier to use `imresize` with the `nearest` method. This performs nearest neighbour interpolation, which is exactly what you are doing above. – rayryeng Jun 05 '17 at 19:41