COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

remove multiple rows of matrix based the value and index of row of another matrix

• Email
• Follow

```I have 2 matrices with the same row, matrixA (n  by k) and matrixB (n by 1)

matrixA =

1              2              3              4
5              6              7              8
9             10             11             12
13             14             15             16
17             18             19             20

matrixB =

1
2
2
1
2

now I would like to remove row of matrix A, based the value of element in matrix B
For example remove all row of matrixA when value of element in matrixB is equal 2
matrixA = removeRow(matrixB)
matrixA =

1              2              3              4
13             14             15             16
Only row 1 and row 4 of matrixA remained.

The code I wrote as a mathlab script:
==========================================
for i=1:length(matrixB)
if matrix(1,i) = 2
index = index+1;
removeRow(index) = i;
end
end

matrixA(removeRow,:)=[]
=========================================
It does not work
???  Index of element to remove exceeds matrix dimensions.
```
 0

See related articles to this posting

```ops!!! matrixB is an array 1xn, not n by 1
matrixB = [1 2 2 1 2]
```
 0

```Hello Vinh,

matrixA(matrixB==2,:) = [];

Best.
```
 0

```"Sadik " <sadik.hava@gmail.com> wrote in message <ho35gj\$g0m\$1@fred.mathworks.com>...
> Hello Vinh,
>
>
> matrixA(matrixB==2,:) = [];
>
> Best.

this is a perfect simplest code - thank you very much ^_^
```
 0