1

I have n vectors of size 1,m, named R1,R2,...,Rn.
I applied this code for the vector R1.

[B,I] =sort([R1 0]);
for q=1:breadth;
    if ismember(q, I(1:3))==1;
        x(q,1)=-1/6;
    elseif ismember(q, I(breadth-1:breadth+1))==1;
        x(q,1)=1/6;
    else
        x(q,1)=0;
    end
end

breadth is the number of columns, in my case it's m.
My question is: How to repeat the code to each vector (from R1 to Rn)?
‏I'm a beginner with matlab, I'm not familiar with code.

output

I will be grateful for any help!

MattSchmatt
  • 850
  • 8
  • 18
  • 2
    Creating variable names that way is called ["Dynamic Variable Naming"](http://stackoverflow.com/questions/32467029/how-to-put-these-images-together/32467170#32467170) and is considered very bad practise. You have to use hacky solutions or hand copying (see Alessiox' answer) to work around this. MATLAB has various data containers which are suited for this kind of storage: matrices, cell arrays and struct arrays. – Adriaan Feb 11 '16 at 13:43
  • well received.. thanks for your comments – Nourhaine Nefzi Feb 11 '16 at 15:12

2 Answers2

1

If the n vectors have the same size (1,m in your case, i.e. they are all row-vectors) you can concatenate them in a single matrix where each row is a vector. This concatenation can be easily done as following:

R_total=[R1;R2;...;Rn];

and then process one-by-one the rows of R_total (i.e. single vectors) in a for-loop:

for i=1:n
    % extract i-th row of R_total: R_total(i,:)
    VectorToBeProcessed=R_total(i,:);

    % start processing such vector
    [B,I] =sort([VectorToBeProcessed 0]);
    for q=1:m
        if ismember(q, I(1:3))==1
            x(q,1)=-1/6;
        elseif ismember(q, I(m-1:m+1))==1
            x(q,1)=1/6;
        else
            x(q,1)=0;
        end
    end
end

The notation R_total(i,:) means the i-th row and all the columns (:) of the matrix R_total.

Obviously there is something you should take care though: the x vector will always be overwritten in the for loop. You should clarify what such vector must contain. If your task is to create one vector x for every vector (R1,...,Rn) than x should be a matrix itself in which the i-th row will contain the results related to the i-th row of R_total.
In this case the above code should be modified as follows:

for i=1:n
    % extract i-th row of R_total: R_total(i,:)
    VectorToBeProcessed=R_total(i,:);

    % start processing such vector
    [B,I] =sort([VectorToBeProcessed 0]);
    for q=1:m
        if ismember(q, I(1:3))==1
            x(i,q)=-1/6;
        elseif ismember(q, I(m-1:m+1))==1
            x(i,q)=1/6;
        else
            x(i,q)=0;
        end
    end
end

So now (in x) we will write every q-th element in a single i-th row, where i also runs the rows of R_total.

Also, as @dasdingonesin pointed out, there's no need for the strcat() function. Such function will return 'R1', 'R2', ...'Rn' as strings. That's simply pointless in your case.

Update: both @Adriaan and @PagMax pointed out that this approach might not be suitable when dealing with tons of arrays to be concatenated into one matrix, since the concatenation is done "manually". This solution is (as instead) fully-automated. Let's suppose that in our Workspace we do have 5 row-vectors of size 5 and we want to concatenate such vectors in a matrix (same fashion: 1 vector = 1 row):

clear all; % clear all variables from the Workspace, starting clean
a=1:5;     % a = [1 2 3 4 5]
b=a+1;     % b = [2 3 4 5 6]
c=b+1;     % c = [3 4 5 6 7]
d=c+1;     % d = [4 5 6 7 8]
e=d+1;     % e = [5 6 7 8 9]

VariableList=whos;

Matrix=[];

for i=1:size(VariableList,1)
    newVector=VariableList(i).name; % newVector is a string containing the name of the i-th variable in the Workspace
    eval(['Matrix=[Matrix ; ' newVector '];']);
end

This code needs some extra words: whos returns a struct containing infos about the variables in the workspace and from this struct we only take the name of such variables. eval, instead, evaluates an expression in string form so basically the concatenation done "manually" in the previous code is here done automatically and in string form. As you'd expect, Matrix has the form:

Matrix =

     1     2     3     4     5
     2     3     4     5     6
     3     4     5     6     7
     4     5     6     7     8
     5     6     7     8     9

Obviously in this simple scenario we have 5 variables in the Workspace that are the 5 vectors that must be concatenated. If you have many variables but you must concatenate just some of them, you must "enrich" the for-loop with some if/else to discriminate between "useful variables" (i.e. that must be concatenated) and "not useful variables". In @Nourhaine Nefzi 's example, where all the vectors' name start with the letter R (and by assuming that there are no other variables whose name start with the letter R) a nice if/else condition can be the following one:

if newVector(1)=='R' %or use strcmp()
    % perform concatenation
else
    % do nothing and jump to the next for-loop iteration (i.e. next variable)
end
AlessioX
  • 3,167
  • 6
  • 24
  • 40
  • thanks for your detailed reply! many thanks.. The code that I put will give me the vector `x` related to the `R1`.. What I want to do is creating a matrix `X` that contains (`x` associated to `R1` , x2 associated to `R2`, ...`xn` associated to `Rn`).. I tried your code but it gives me one vector x (as u told me) – Nourhaine Nefzi Feb 11 '16 at 13:53
  • I have edited the answer. Check if that's working for you – AlessioX Feb 11 '16 at 14:01
-2

wrap it around in another for loop

for j=1:n
  rvec=eval(strcat('R',num2str(j)))
  [B,I] =sort([rvec 0]);
 for q=1:breadth;
  if ismember(q, I(1:3))==1;
    x(q,1)=-1/6;
  elseif ismember(q, I(breadth-1:breadth+1))==1;
    x(q,1)=1/6;
  else
    x(q,1)=0;
  end
 end
end
PagMax
  • 8,088
  • 8
  • 25
  • 40
  • Thanks for your help.. I tried the code but it doesn't work, I edit my question by adding a picture that shows the output.. – Nourhaine Nefzi Feb 11 '16 at 11:22
  • 2
    How is creating a string containing the name of the vector supposed to help? – dasdingonesin Feb 11 '16 at 11:26
  • The code is applied for R1 and the question says it needs to be used for R2, R3,...Rn. Also if you have a concern, you can be specific about what is wrong. Simply asking 'how is it supposed to help?' with no details is counter productive. I at least tried to answer without making hostile comment. – PagMax Feb 11 '16 at 15:24
  • I can see Alessoix answer has worked for you but I still do not get why creating R1, R2, R3..in loop is pointless. R_total vector is creating a list of these vectors manually anyway and after that the process is identical. Instead of creating R_total vector, I am directly calling each vector at a time. Not sure why is it called 'pointless' – PagMax Feb 11 '16 at 15:38
  • @PagMax, your code is not complete. It just creates a string with the variable name without going into much detail on how to use such string. I have updated my answer with a fully-automated approach. – AlessioX Feb 12 '16 at 09:05
  • @Alessiox yes the code is not complete but I have put a comment that same code needs to be repeated. One would just have to copy paste the same code which is posted and replace R1 by rvec which will update every time. – PagMax Feb 12 '16 at 09:40
  • @PagMax, dude `rvec` is a string containing the vector name. You want to sort an array given by the concatenation of the string `'R1'` (let's say) and the scalar `0`. The problem is not the loop, the problem is this string. How can a string possibly be helpful? The code must work on the vector itself, not on its name – AlessioX Feb 12 '16 at 10:04
  • @Alessiox, got it. Well then won't simply adding an eval function like you have used will help ? I updated my answer again. – PagMax Feb 12 '16 at 10:10