## Sequence within a sequence

Scott

### Scott (view profile)

さんによって質問されました 2012 年 3 月 28 日
Wayne King

### Wayne King (view profile)

さんの 回答が採用されました
Hi I want to be able to create a sequence as such
j=1111,2222,3333,4444
It is for use in a loop where j will jump up by 1 for every forth value of i.
thanks

#### 0 件のコメント

サインイン to comment.

## 4 件の回答

### Wayne King (view profile)

on 28 Mar 2012

Another way (you need Signal Processing Toolbox)
x = 1:4;
y = upsample(x,4);
h = ones(4,1);
y = filter(h,1,y);

Daniel Shub

### Daniel Shub (view profile)

on 28 Mar 2012
Hey for once my solution is faster ...
n = 1e7;
m = 10;
tic
x = 1:n;
y = upsample(x,m);
h = ones(m,1);
y = filter(h,1,y);
toc
tic
z = reshape(repmat(1:n, m, 1), n*m, 1);
toc
Elapsed time is 5.215208 seconds.
Elapsed time is 3.261213 seconds.
Daniel Shub

### Daniel Shub (view profile)

on 28 Mar 2012
Despite my answer being faster (and I think simpler), I like your answer better from a theoretical vantage.
Wayne King

### Wayne King (view profile)

on 28 Mar 2012
:) I don't doubt that at all!

サインイン to comment.

### Daniel Shub (view profile)

on 28 Mar 2012

n = 5;
m = 3;
reshape(repmat(1:n, m, 1), n*m, 1)

#### 0 件のコメント

サインイン to comment.

### Dr. Seis (view profile)

on 28 Mar 2012

I thought I remembered a version by Walter using cumsum, but I couldn't find the link. Here's my attempt to recreate:
n = 5;
m = 3;
a = zeros(m,n);
a(1,:) = 1;
b = cumsum(reshape(a,1,numel(a)));
Running Daniel's test, I got ~0.65 seconds and ~1.05 seconds for the reshape&cumsum and repmat&reshape versions, respectively. However, what you save in compute time is eaten up by doing extra typing.

Daniel Shub

### Daniel Shub (view profile)

on 28 Mar 2012
+1 I knew my answer wouldn't be the fastest. Not only do you have a faster answer, but you also have a faster computer (your answer is faster on my computer also).

サインイン to comment.

### Scott (view profile)

on 28 Mar 2012

excellent that did answer my question however I am trying to use this as a simultaneous loop. My guess is using the parfor command:
function [x]= displacementsi(i,z,nx,ny) m=5 n=3
parfor
j=reshape(repmat(1:n, m, 1), n*m, 1)
x(1:4)=z((2*i)-(j-2):(2*i)+(j+1))
end end
I get this error
??? Error: File: displacementsi.m Line: 5 Column: 8 Expression or statement is incomplete or incorrect.
Error in ==> fe_test at 112 [x] = displacementsi(i,displacements,nx,ny);

Daniel Shub

### Daniel Shub (view profile)

on 28 Mar 2012
This is a new question and should be asked as such.
Scott

### Scott (view profile)

on 28 Mar 2012
ok sorry

サインイン to comment.

Translated by