Say I have a nxm
matrix and want to treat each row as vectors in a function. So, if I have a function that adds vectors, finds the Cartesian product of vectors or for some reason takes the input of several vectors, I want that function to treat each row in a matrix as a vector.

- 60,010
- 15
- 145
- 220

- 11
- 3
-
2This is difficult to answer as it's such a general question. `A(i,:)` gives you the i-th row of a matrix as a vector, and depending on your circumstances you could use a `for`-loop, or maybe `arrayfun`. – David Sep 09 '14 at 01:35
-
Sorry, I tried to make it general enough to be applicable to everyone. I have a function called setprod that takes the cartesian product of 2 or more vectors. I want to enter a large matrix into that function and have it read each row as one vector. Is this more clear? – user2738036 Sep 09 '14 at 01:40
-
Do you want to take the product of every pair of vectors, or just some vectors with some other vectors? – David Sep 09 '14 at 01:40
-
The cartesian product of all the vectors. So if [1 2] [3 4] [5 6] are my vectors, I want [1 3 5, 1 3 6, 1 4 6....] where the input to the function was [1 2; 3 4; 5 6] – user2738036 Sep 09 '14 at 01:45
-
So I think you'll need to *EDIT* your question and add the **FULL** output for your example input in your previous comment in the exact shape you want (i.e. so far your output is just a row vector and you're mixing up spaces and commas which used like you have just do the same thing :/). This is how to do a cartesian product in matlab: http://stackoverflow.com/questions/9834254/cartesian-product-in-matlab. To do an operation on row-wise pairs, you can use `pdist2`, but probably not for sets, otherwise check out using `nchoosek(1:n,2)` to create pair-wise indices for looping – Dan Sep 09 '14 at 06:02
-
There is no general "treat this matrix as row-wise vectors" semantic in MATLAB. It is generally simple to express it directly, but the syntax or approach depends on what you're trying to do. – Peter Sep 09 '14 at 12:50
4 Answers
This sounds like a very operation in Matlab. You can access the ith
row of a matrix A
using A(i, :)
. For example, to add rows i
and j
, you would do A(i, :) + A(j, :)
.

- 8,935
- 1
- 38
- 77

- 590
- 3
- 19
-
I'd like to treat EVERY row as a vector. So say I wanted to edit rows i THROUGH j, how would I do that? – user2738036 Sep 09 '14 at 01:29
Given an nxm matrix A:
- If you want to edit a single column/row you could use the following syntax: A(:, i) for the ith-column and A(i, :) for ith-row.
- If you want to edit from a column/row i to a column/row j, you could use that syntax: A(:, i:j) or A(i:j, :)
- If you want to edit (i.e.) from the penultimate column/row to the last one, you could you: A(:, end-1:end) or A(end-1:end, :)
EDIT: I can't add a comment above because I don't have 50 points, but you should post the function setprod. I think you should be able to do what you want to do, by iterating the matrix you're passing as an argument, with a for-next statement.

- 945
- 8
- 19
-
Here is the function: http://www.mathworks.com/matlabcentral/fileexchange/5898-setprod – user2738036 Sep 09 '14 at 02:11
I think you're going to have to loop:
Input
M = [1 2;
3 4;
5 6];
Step 1: Generate a list of all possible row pairs (row index numbers)
n = size(M,1);
row_ind = nchoosek(1:n,2)
Step 2: Loop through these indices and generate the product set:
S{n,n} = []; //% Preallocation of cell matrix
for pair = 1:size(row_ind,1)
p1 = row_ind(pair,1);
p2 = row_ind(pair,2);
S{p1,p2} = setprod(M(p1,:), M(p2,:))
end

- 45,079
- 17
- 88
- 157
Transform the matrix into a list of row vectors using these two steps:
- Convert the matrix into a cell array of the matrix rows, using
mat2cell
. - Generate a comma-separated list from the cell array, using linear indexing of the cell contents.
Example: let
v1 = [1 2];
v2 = [10 20];
v3 = [11 12];
M = [v1; v2; v3];
and let fun
be a function that accepts an arbitrary number of vectors as its input. Then
C = mat2cell(M, ones(1,size(M,1)));
result = fun(C{:});
is the same as result = fun(v1, v2, v3)
.

- 110,752
- 13
- 76
- 147