Clear Filters
Clear Filters

how to extract rows and leave zeros behind?

3 views (last 30 days)
Dear All, I have a big matrix called A, I extract another matrix from this A and call it B based on a condition of the value x in the matrix A, I wrote this code but it doesnt work well, can any body please help fix it? I need to extract the new matrix B and leave the rows as zeros in the original matrix A.
B= A(x(:,1)>9.84 & x(:,1)<12.47 , :);
A(find(A(B,:)))=0;

Accepted Answer

Walter Roberson
Walter Roberson on 11 Nov 2017
B= A(x(:,1)>9.84 & x(:,1)<12.47 , :);
leaves B as a subset of A, having selected some of the rows but retaining all columns.
A(B,:)
then uses that subset as an index into A. That is mostly going to fail as the entries are probably not zero.
Try
mask = x(:,1)>9.84 & x(:,1)<12.47;
B = A(mask, :);
A(mask, :) = 0;
  1 Comment
MAHMOUD ALZIOUD
MAHMOUD ALZIOUD on 11 Nov 2017
Ok Mr Walter I will give it a try, this looks very correct and simple. Thank you very much

Sign in to comment.

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!