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