rowfun in Matlab: why doesnt this work? [duplicate] - matlab

This question already has answers here:
Vector norm of an array of vectors in MATLAB
(4 answers)
Closed 7 years ago.
I know quite well how to use R but i'm new to Matlab
suppose I have the simple matrix
y =
1 2
3 4
5 6
i want to compute row by row the (euclidean) norm of the vector rows. and return it in a column
vector.
>> norm(y(1,:))
ans = 2.2361
but when i put the following command, i get an error... whats wrong ?
>> rowfun(norm,y)
Error using norm
Not enough input arguments.

you are looking for the norm of every row, you can do this by using arrayfun instead of rowfun as rowfun is more used in table structures. The 1 liner is:
result = arrayfun(#(idx) norm(y(idx,:)), 1:size(y,1));
result =
2.2361 5.0000 7.8102

Related

Selecting Matrix element using vector elements [duplicate]

This question already has answers here:
Use a vector as an index to a matrix
(3 answers)
Closed 4 years ago.
Is there a way in Matlab to select a matrix element based off the elements in a vector? I don't think my description is clear, but what I effectively want to do is something similar to:
A=zeros(3,3,3) %3d matrix
A(1,1,2)=5
b=[1,1,2]
A(b)=5
Meaning, some easy way to select one element from a matrix using the entries in a vector as arguments. This exact example does not work because the last line counts b as a single argument, not three. I could write A(b(1),b(2),b(3)) but what I'm really looking for here is if there's a nice way of doing.
Method 1: Use sub2ind to find the linear index
You can define a function called findLinearIndex such that it convert the vector elements to the linear index of A:
findLinearIndex = #(A,b) sub2ind(size(A), b(1), b(2), b(3))
A(findLinearIndex(A,b)) = 5
Method 2: Convert the vector to cell array by num2cell
Then, you can use {:} to get the index
b_cell = num2cell(b) ;
A(b_cell{:}) = 5

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.

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)

Find the value in a matrix with certain condition [duplicate]

This question already has answers here:
Find vector elements matching two conditions in Matlab
(3 answers)
Closed 9 years ago.
How to find the value in a matrix with certain condition. For example,
a=[-3.14,2.12,-5,3,6,7];
b=find(a>0)
this will return the indices of matrix with that ">0" condition, which is b= 2 4 5 6.
do we have any solution for find the actual value in a matrix under that condition, such as returning b= 2.12 3 6 7 ?
you can even skip the find part:
whatyouwant = a(a>0);
That's called logical indexing in Matlab...
You could do the following
a = [-3.14,2.12,-5,3,6,7];
b = find(a>0)
c = a(b)
The c would then be the selected values based on the indices in b.
Hope it helps!

access to values in vector in matlab [duplicate]

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))