How to "stretch" matrix

71 次查看(过去 30 天)
Daniel
Daniel 2012-4-21
Hello,
I was wondering if MATLAB had a function for doing the following. Say I have the following vector:
[1 1 2 2 0 0]
And I want to make a new vector which contains 1.5 times the amount of the present elements, i.e. "stretch" it by 1.5
[1 1 1 2 2 2 0 0 0]
Just asking before writing any buggy, inneficient code.
regards,
Daniel
  2 个评论
James Tursa
James Tursa 2012-4-21
Is it always adding one more element of each? Or by "1.5 times" do you mean that you have a larger problem in mind like [1 1 1 1 2 2 2 2 0 0 0 0] etc and would like the solution to be [1 1 1 1 1 1 2 2 2 2 2 2 0 0 0 0 0 0] etc? I.e., how generic is your real problem?
Walter Roberson
Walter Roberson 2012-4-21
Will the number of identical elements in a row always be the same?

请先登录,再进行评论。

回答(5 个)

Image Analyst
Image Analyst 2012-4-21
Daniel, if you have the Image Processing Toolbox, you can do it in one single, and very simple, line of code:
% Create sample data.
m1 = [1 1 2 2 0 0]
% Now do the replication like Daniel wants.
m2 = imresize(m1, [1 9], 'nearest')
In the command window:
m1 =
1 1 2 2 0 0
m2 =
1 1 1 2 2 2 0 0 0
Of course you can change the 9 to be any length you want your output vector to be.

Richard Brown
Richard Brown 2012-4-21
Further to Image Analyst's answer, you can do it without the image processing toolbox too (assuming m has an even number of entries)
m = [1 1 2 2 0 0 ];
n = numel(m);
m2 = interp1(1:n, m, linspace(1, n, 1.5*n), 'nearest')

Pippa Williams
Pippa Williams 2020-2-11
Another (perhaps simpler) option if you have R2015a or later: repelem ("repeat elements") will also do.
  3 个评论
Image Analyst
Image Analyst 2023-9-19
@Dana Monahan repelem doesn't do what the original poster asked, as far as I can see. @Daniel wants to "stretch" (resize) the array by 1.5 times. repelem() takes each element and makes duplicate elements. So, if you wanted to add one copy, it would insert the first 1 one time, then add a second 1 for the second 1, then add a third 1 for the third one. So repelem would give 4 ones, not 3:
% Create sample data.
m1 = [1 1 2 2 0 0];
% Now use repelem to TRY to generate [1 1 1 2 2 2 0 0 0]:
m2 = repelem(m1, 2) % Not what was wanted
m2 = 1×12
1 1 1 1 2 2 2 2 0 0 0 0
If you gave a vector for the number of times to replicate the element you could do it:
m3 = repelem(m1, [1,2, 1,2, 1,2])
m3 = 1×9
1 1 1 2 2 2 0 0 0
but that depends on you knowing in advance which elements should be replicated and which should not. I guess you could use a for loop to try to generate the [1,2, 1,2, 1,2] vector, but that gets complicated.
Dana Monahan
Dana Monahan 2023-9-25
编辑:Dana Monahan 2023-9-25
@Image Analyst, I appreciate the reply. I was rooting through this thread for a HW assignment for college. I actually ended up settling on a different solution as repelem doesn't allow for lowering the size of a matrix and because my proffesor asked I use the tools we had learned in class. I ended up using this:
function [ output ] = vecResize( v , multiplier )
%output = vecResize(v, multiplier) Streches/Compacts the given vector "v" by the given scalar "multiplier"
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
s = length(v);
ns = ceil(s * multiplier);
index = round(linspace(1, s, ns));
output = v(index);

请先登录,再进行评论。


Andrei Bobrov
Andrei Bobrov 2012-4-21
a = [1 1 2 2 0 0];
t = 1.5
k=[true,diff(a)~=0];
k2 = find(k);
k3 = [k2(2:end)-1 numel(k)];
k4 = k3-k2+1;
m = round(k4*t);
if all(diff(m) == 0)
out = reshape(ones(m(1),1)*a(k),1,[]);
else
out = cell2mat(arrayfun(@(x,y)x(ones(1,y)),a(k),m,'un',0));
end
ADD on Walter's comment
out = kron(a(1:2:end),ones(1,t*2))
  2 个评论
Walter Roberson
Walter Roberson 2012-4-21
You forgot the kron() call :-)
Andrei Bobrov
Andrei Bobrov 2012-4-21
Thank you Walter!

请先登录,再进行评论。


Ryan Jones
Ryan Jones 2016-12-7
You can also use repmat and indexing. If n is + integer than simply:
m = [1 1 2 2 0 0];
temp = repmat(a,n,1)
m2 = temp(1:end)
for your particular example where you want a resizing of 3/2:
%n1/n2
n1 = 3, n2 =2
m = [1 1 2 2 0 0];
temp = repmat(a(1:n2:end),n1,1)
m2 = temp(1:end)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by