MATLAB Answers

Delete and merge rows and columns based on values of other matrix

1 view (last 30 days)
vimal r
vimal r on 21 Feb 2020
Commented: Guillaume on 24 Feb 2020
suppose consider a matrix of A = [1 2 3 4 ; 5 6 7 8 ; 9 1 2 3 ; 1 2 3 4] consider matrix B = [ 1 0 2 4 ]
so in matrix B value of 2 unit is 0 so i need to delete second row and second column in matrix A
final A1 matrix = [1 3 4 ; 9 2 3 ; 1 3 4]

  0 Comments

Sign in to comment.

Accepted Answer

Sindar
Sindar on 21 Feb 2020
If I understand correctly:
  • You have one NxN matrix A
  • And a second 1xN vector B
  • When an element of B is zero, you delete rows and columns from A with the same index
A = [1 2 3 4 ; 5 6 7 8 ; 9 1 2 3 ; 1 2 3 4];
B = [ 1 0 2 4 ];
% find where B is nonzero
idx = find(B~=0);
% copy corresponding rows and columns of A to A1
A1=A(idx,idx);

  7 Comments

Show 4 older comments
Guillaume
Guillaume on 24 Feb 2020
If you're on R2019b or later, replace readtable by readmatrix so d1 and rd are matrices instead of tables. The rest of your code will then work properly.
Or in any version you can continue importing as a table but then you have to use the correct indexing to get data out of a table:
sen = rd{1, :}; %{} to extract data out of a table.
Guillaume
Guillaume on 24 Feb 2020
I'm not sure which of your 3 questions the above refers to. You would be better off starting a new question for each topic.

Sign in to comment.

More Answers (0)

Sign in to answer this question.