Remove 0 values from an array

조회 수: 5 (최근 30일)
Chris Drew
Chris Drew 2022년 3월 9일
답변: Rik 2022년 3월 9일
I've read in data from an Excel spreadsheet consiting of 7 rows and 3 columns.
In each colum there is a 0 value (column 1, row 7; column 2, row 2; column 3, row 4).
Is there a way to remove these values from the array (without modifying the initial data that is brought in from Excel)?
  댓글 수: 3
DGM
DGM 2022년 3월 9일
편집: DGM 2022년 3월 9일
Even if there is only one zero value per column, removing them and collapsing vertically will mean your data is misaligned in the region where values were removed.
For example:
% create a patterned array
A = (1:10)' + (10:10:30);
A([3 15 27]) = 0
A = 10×3
11 21 31 12 22 32 0 23 33 14 24 34 15 0 35 16 26 36 17 27 0 18 28 38 19 29 39 20 30 40
% remove zeros and reshape
A = reshape(A(A~=0),[9 3])
A = 9×3
11 21 31 12 22 32 14 23 33 15 24 34 16 26 35 17 27 36 18 28 38 19 29 39 20 30 40
% note the loss of alignment in the middle
all(range(mod(A,10),2)==0,2)
ans = 9×1 logical array
1 1 0 0 0 0 1 1 1
Chris Drew
Chris Drew 2022년 3월 9일
I see what you mean, maybe I have explained it incorrectly.
For the rows where there is a zero value, that entire row should be ignored/removed.
Is there a way to do this, to tell MatLab to ignore any row where there is a 0 value?

댓글을 달려면 로그인하십시오.

답변 (1개)

Rik
Rik 2022년 3월 9일
% create a patterned array
A = (1:10)' + (10:10:30);
A([3 15 27]) = 0;
row_has_zero = any( A==0 ,2)
row_has_zero = 10×1 logical array
0 0 1 0 1 0 1 0 0 0
A(row_has_zero,:)=[]
A = 7×3
11 21 31 12 22 32 14 24 34 16 26 36 18 28 38 19 29 39 20 30 40

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by