Suppose I've got the following vector and plot it from index 1 to 11:
a = [1 2 5 4 5 4 2 3 7 1 5];
plot(a);
How can I plot only part of this vector? like from index 3 to index 7. I found this question but couldn't figure out how to use it.
a = [1 2 5 4 5 4 2 3 7 1 5];
plot(a(3:7));
I have set of arrays:
x1=[1 2 3 4 5 6 7 8 9];
y1=[1 2 3 4 5 6 7 8 9];
z1=[2 2 2 2 2 9 6 2 2];
and
x2=[6 7];
y2=[6 7];
z2=[2 2];
by tracking x y coordinates, the z arrays have to be subtracted so that output will be
x=[1 2 3 4 5 6 7 8 9];
y=[1 2 3 4 5 6 7 8 9];
z=[2 2 2 2 2 7 4 2 2];
You can get the indicies of your elements in z using the ismember function:
a1=[x1.',y1.'];
a2=[x2.',y2.'];
[~,ix]=ismember(a2,a1,'rows')
z1(ix)=z1(ix)-z2
To use the ismember two matrices a1 and a2 are created witch contain the coordinates in rows. Then ismember with 'rows' option is used to get the indices.
When a point exists in x2/y2 which does not exist in x1/y1 the above code will fail.
I'm very new to scilab syntax and can't seem to find a way to extract the even and odd elements of a matrix into two separate matrix, suppose there's a matrix a:
a=[1,2,3,4,5,6,7,8,9]
How do I make two other matrix b and c which will be like
b=[2 4 6 8] and c=[1 3 5 7 9]
You can separate the matrix by calling row and column indices:
a=[1,2,3,4,5,6,7,8,9];
b=a(2:2:end);
c=a(1:2:end);
[2:2:end] means [2,4,6,...length(a)] and [1:2:end]=[1,3,5,...length(a)]. So you can use this tip for every matrix for example if you have a matrix a=[5,4,3,2,1] and you want to obtain the first three elements:
a=[5,4,3,2,1];
b=a(1:1:3)
b=
1 2 3
% OR YOU CAN USE
b=a(1:3)
If you need elements 3 to 5:
a=[5,4,3,2,1];
b=a(3:5)
b=
3 2 1
if you want to elements 5 to 1, i.e. in reverse:
a=[5,4,3,2,1];
b=a(5:-1:1);
b=
1 2 3 4 5
a=[1,2,3,4,5,6,7,8,9];
b = a(mod(a,2)==0);
c = a(mod(a,2)==1);
b =
2 4 6 8
c =
1 3 5 7 9
Use mod to check whether the number is divisible by 2 or not (i.e. is it even) and use that as a logical index into a.
The title is about selecting rows of a matrix, while the body of the question is about elements of a vector ...
With Scilab, for rows just do
a = [1,2,3 ; 4,5,6 ; 7,8,9];
odd = a(1:2:$, :);
even = a(2:2:$, :);
Example:
--> a = [
5 4 6
3 6 5
3 5 4
7 0 7
8 7 2 ];
--> a(1:2:$, :)
ans =
5 4 6
3 5 4
8 7 2
--> a(2:2:$, :)
ans =
3 6 5
7 0 7
Please is there a matlab code to subtract two vector values where one value is always the larger?
Eg;
A=[10 9 8 7 6 5 4 3 2 1]; B=[5 4 3 2 1 10 9 8 7 6];
I want to subtract these two vectors where the minuend is always the larger so that the answer will be:
[5 5 5 5 5 5 5 5 5 5]
How can I do this?
Use the following code: abs(A-B)
Lets say I have got vector1:
2
3
5
6
7
9
And a vector2:
1
2
3
Now I would like to obtain the following matrix:
2 1
3 2
5 3
6 1
7 2
9 3
That is, I want to add vector2 as a column next to vector1 until the new column is completely filled. I have to do this with a lot of vectors with different sizes. The only thing I know in advance is that the length of vector1 is an integer multiple of the length of vector2.
Any suggestions?
Use repmat to replicate the smaller matrix.
a = [2 3 5 6 7 9]';
b = [1 2 3]';
c = [a repmat(b, length(a) / length(b), 1)]
Result:
c =
2 1
3 2
5 3
6 1
7 2
9 3
You can then replicate the vector:
[vector1, repmat(vector2,n,1)]
where n is your multiple of vector2.
This could be an alternative
[x [y'; y']]