## help solving [2:1] matrix problem

### khalid (view profile)

on 11 Mar 2014
Latest activity Edited by Patrik Ek

### Patrik Ek (view profile)

on 12 Mar 2014

i am doing a resampling problem in which i am trying to assign xp(j) to x(i) but the problem is both my xp and x are [2:1] ... how can i solve this problem .. please help ?? Thanks

``` for i = 1 : N
u = rand; % uniform random number between 0 and 1
qs = 0;
for j = 1 : N
qs = qs+ q(j);
if qs >= u
x(i)= xp(j);
break;
end
end
end```

Patrik Ek

### Patrik Ek (view profile)

on 11 Mar 2014

what do you mean with [2:1] a vector from 2 to 1 increasing by 1 so that you get [2:1] = 2? Some more input would be appreciated, I am not really sure what your problem is. Especially since your code is not stand alone. I have no idea what qtempsum (which does not even seem to be temp?!) or q(j), if you tries to overwrite x(i) xp(j) or if they a zero otherwise,... From the code I would assume x(i) and xp(j) are scalar, but you question makes me confused there. Also, in general I would begin to advise you to get rid of the for loops in some way, but since I am unsure of your problem, I cannot tell.

khalid

### khalid (view profile)

on 12 Mar 2014

thank you for replying ... sorry 'qtempsum' was qs (i made the correction) ... q(j) is an array ... by [2:1] i mean x and xp are m*n matrices with size 2*1 ... x and xp are non zero ... here in this part i am reassigning the xp to x .... Hope it helps you understand my problem ... Thanks again ... waiting for your reply

Patrik Ek

### Patrik Ek (view profile)

on 12 Mar 2014

Still makes no sense. If there are m x n 2x1 matrices, how are they stored? In a cell? There are issues in the code since notation like x(i) (read do not know anyhing about size and class for many of the variables) refers to a single matrix element in matlab, that I cannot even say where to start debugguing. If you want help you need to include x and xp in the example. The problem is related to size and indexing and I cannot help you if do not specify the variables you want to manipulate. It is like saying, "I have a dog, where do I live?". Please give something like,

``` xp = ?; % xp is n x m matrices somehow implies a cell here, but you
x = ?; % do not use syntax for cell access which is confusing, which is
q = ?; %  why these must be stated. Simplified code require correct syntax.
for i = 1 : N
u = rand; % uniform random number between 0 and 1
qs = 0;
for j = 1 : N
qs = qs+ q(j);
if qs >= u
x(i)= xp(j);
break;
end
end
end```

## Tags

No tags are associated with this question.

## Products

No products are associated with this question.