Delete zeros form multiple columns in a matrix

How do I reduce a matrix with multiple initial zeros
A=[0 0 0 0 0 ; 0 0 0 0 1 ; 0 0 1 0 1 ; 0 1 1 0 1 ; 0 1 1 1 1; 1 1 1 1 1; 1 1 1 1 1 ]
to
A = [1 1 1 1 1
1 1 1 1 1
1 1 1 1
1 1 1
1 1
1]
Basically delete all zero from all the column

답변 (2개)

the cyclist
the cyclist 2021년 5월 8일

0 개 추천

A numeric data type cannot have empty elements.
You have a couple options. You could use a cell array, which can have empty elements. Or perhaps you could replace the zeros with NaN.
What are you planning on doing as a next step with the result?

댓글 수: 2

The thing is the non zero values are voltage data of 5 batteries and I need to plot many such data. I want to delete the initial zeros so that the graph is correct and the zeros are a problem to my further calculation too.
DGM
DGM 2021년 5월 9일
If you don't want zeros to plot, just replace them with NaN. NaN values don't plot.
For actual handling of electrical signal data, I can't imagine a scenario where zeros are categorically meaningless. If you're running into problems in further calculations, I doubt that stripping all zeros is the appropriate solution.

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

Image Analyst
Image Analyst 2021년 5월 8일

0 개 추천

I agree with the cyclist in that you probably don't really need to do this in your next step, whatever it is.
That said, you can use sparse() but I really, really doubt you want to, or need to, do that.
If you just want to print it out to the command line, you can use a loop with fprintf() and just print those that are 1 and print a space for the zeros.
But a matrix cannot have "holes" or "ragged edges". There has to be something there.

댓글 수: 2

Ok . Thanks both of you . I will then store it in different matrix then . I think that will solve the problem
How are you going to do that? And how will that solve the "problem"? Again, I don't know why there is a "problem" in the first place and you can't just use your original A matrix.

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

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품

릴리스

R2019b

질문:

2021년 5월 8일

댓글:

DGM
2021년 5월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by