access to values in vector in matlab [duplicate] - matlab

This question already has answers here:
MATLAB - extracting rows of a matrix
(5 answers)
Closed 10 years ago.
I have a Nx2 vector, each row in the vector is a coordinate in a matrix.
For example: vector that call Path look like this:
Path=[1 2;
3 4;
5 6;
7 8;];
My question is how can I access the vector to take my x and y coordinates?
If I write Path(1) the answer is 1, and for Path(2) the answer is 3, But I want to take the pairs 1 2, then 3 4 etc..
Can I do it in a loop?
thanks!

This will give you every row one by one.
for i=1:size(Path,1)
Path(i,:)
end
If you just want to plot the path, try:
plot(Path(:,1),Path(:,2))

Related

How to transpose rows into columns in specific order in MATLAB? [duplicate]

This question already has an answer here:
How to convert the elements of a matrix into a single vector
(1 answer)
Closed 5 years ago.
I have a data (the name of data is testdata) in work-space 60x5 double. I have 60 different measurement and 5 samples. To calculate in R, I need to save them in an order that my R code can use them. What I need to do is transposing every 5 rows of each column to one row and adding the next transposed rows under those 5 values. You can find an image of what I want to do. As seen in the image, black rectangle should be transposed to column, then red rectangle transpose and added under the columns which is already used for the first step. I need to do this 60 times so at the end it should be 300x1 double. I hope someone can help me to solve this issue out. Thank you for your time and help.
Best Regards,
See if this is what you want:
A = magic(3); % example matrix
B = A.'; % transpose
B = B(:); % linearize in column-major order
This transforms
A =
8 1 6
3 5 7
4 9 2
into
B =
8
1
6
3
5
7
4
9
2
Reshape will do exactly what you are looking for:
A = magic(5); % Example matrix
B = reshape(A',[],1); % Reshapes the matrix to one vector.

What is dot multiplication [duplicate]

This question already has answers here:
Return Unique Element with a Tolerance
(7 answers)
Closed 5 years ago.
When multiplying matricies what is the difference when there is a dot there? Can this give different results
You can just use the built-in Matlab function uniquetol to obtain the unique values up to some tolerance in the array and ask the length of the returned array.
Example
A = [1+1e-11 2 3 4 1 2 3]; % generate an array with 4 unique values except for some tolerance
length(uniquetol(A, 1e-10)) % will return 4

MATLAB: fast creation of vector of indices [duplicate]

This question already has answers here:
Run-length decoding in MATLAB
(4 answers)
Closed 6 years ago.
I have a variable distr=[0 3 1 0 2];, and I have a variable full which should contrain distr(i) times i, for all i.
In this example, i want:
full=[2 2 2 3 5 5];
because distr(2)=3, therefore 3x 2, and so on.
Of course I can do it in a for-loop:
full=zeros([1,sum(distr)]);
cc=1;
for i=1:length(distr)
curr=distr(i);
full(cc:cc+curr-1)=i*ones([1,curr]);
cc=cc+curr;
end
but that is very slow. Do you know of a fast way, using MATLAB's awesome array-oriented style? Thanks!
Not sure, but maybe this will work. I can't check it since I currently don't have MATLAB:
full_tmp = arrayfun(#(i,n) i*ones(1,n),1:length(distr),distr,'uniformoutput',false);
full = cat(2,full_tmp{:});

Is there anyway to avoid loop in matlab array accessing? [duplicate]

This question already has answers here:
How can I change the values of multiple points in a matrix?
(3 answers)
Closed 6 years ago.
I have a 2-D array. And I want to access it with rows and colums index stored in another 2-D array.
Example: Now I don't want to use loops but I want to access A(1, 2) and A(3, 4).
A = ones(10,10)
B = [1, 2 ; 3, 4]
If I do A(b(:,1), b(:,2)), this will result in all possible combination of [1,2] and [3,4].
How can it be done?
Use MATLAB's sub2ind function:
A(sub2ind(size(A),B(:,1),B(:,2)))

Matlab - sum all elements above current [duplicate]

This question already has an answer here:
matlab based program where output comprises of sum of rows of input
(1 answer)
Closed 8 years ago.
I am trying to implement a vectorised solution in matlab for adding all elements above the current element in a vector. For eg.
I have a vector a as follows
a =
1
2
3
4
I would like a vector b like
b =
1
3
6
10
I know this can be done very easily using a loop but I was wondering if there are indexing options which can allow me to do the same in matlab/ octave?
You can use the Cumulative Summation function (cumsum):
b = cumsum(a)