I want to add a vector to an existing matrix.
Example:
matrix=[1 2 3
4 5 6
0 7 0]
vector = [7
8]
So the target is to find the equal number of vector and matrix for example with:
ismember(matrix,vector)
After that the vector should insert into the matrix like the following:
matrix=[1 2 3
4 5 6
0 7 0
0 8 0]
Instead of using ismember
, you can better use find
with two output arguments:
>> [row, col]=find(matrix==vector(1))
row =
3
col =
2
Using Matlab's automatic matrix expansion, and assuming the vector is a column vector (you can adjust the code accordingly):
>> matrix(row:(row+length(vector)-1),col) = vector
matrix =
1 2 3
4 5 6
0 7 0
0 8 0
If the match is not at the edge (ie, row~=size(matrix,1)
), this would not work though, as the vector would override other entries.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.