user2952993
user2952993

Reputation: 9

For loop iteration

n=5; 
h=[1;2;3;4];  
x = [zeros(1,n) randn(1,n) zeros(1,n)];
t(1,:) = [x(n+1:-1:(length(h)-1))];


for k=2:n
    t(k,:) = [x(n+k:-1:(length(h)-1))];  
end

Is there something wrong in the for loop? as my 1st iteration had no problem creating my row vector but when it comes in the for loop i could seem it make the loop run and the error msg i get is 'Subscripted assignment dimension mismatch.' I can't seem to find the mistake

For my case my final output i am should get is as follows

[x6 x5 x4 x3; x7 x6 x5 x4; x8 x7 x6 x5;x9 x8 x7 x6;x10 x9 x8 x7;x11 x10 x9 x8]

Upvotes: 1

Views: 140

Answers (3)

Luis Mendo
Luis Mendo

Reputation: 112679

You can do it more easily with bsxfun:

n = 5;
k = 4; % number of columns of result
x = [zeros(1,n) randn(1,n) zeros(1,n)]; % your data

t = x(bsxfun(@plus, (1:n+1).', n:-1:n-k+1 )); % result

Upvotes: 1

am304
am304

Reputation: 13876

I think you probably want something like that:

t = zeros(n+1,length(h)) % pre-allocate t to an array of zeros of the correct size
for k=1:n+1
    t(k,:) = x(n+k:-1:length(h)+k-2);
end

Obviously check the indices are correct (I think they are).

Upvotes: 1

Dennis Jaheruddin
Dennis Jaheruddin

Reputation: 21563

When I run it till the point where it is stuck, this is what I see:

You have a variable t of size 1x4

You try to append a row below it of size 1x5

Obviously it won't fit.

Judging from your description I would say the second row is too long.

Upvotes: 2

Related Questions