Matrix split in vector

조회 수: 1 (최근 30일)
steve Brian
steve Brian 2020년 4월 7일
댓글: Stephen23 2020년 4월 7일
Hello everyone! I need a little help, I'm at the beginning and I would like to create a function that receives a matrix with positive elements (0 included) and returns in a vector line by line the elements of the matrix without zeros. A little help, please?
ex: A = [1 2 0 3; 0 0 4 5; 6 0 7 8]
newA= [1, 2, 3, 4, 5, 6, 7, 8]

채택된 답변

Stephen23
Stephen23 2020년 4월 7일
편집: Stephen23 2020년 4월 7일
>> B = nonzeros(A.')
B =
1
2
3
4
5
6
7
8
  댓글 수: 2
steve Brian
steve Brian 2020년 4월 7일
편집: steve Brian 2020년 4월 7일
and if i want B to be in row?
I have something like this:
function [values] = ceva(A)
B = A(A>0)
values = B';
endfunction
when i insert A, only display B = columne
edit: i fix it
Stephen23
Stephen23 2020년 4월 7일
"and if i want B to be in row?"
nonzeros(A.').'

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

추가 답변 (1개)

KSSV
KSSV 2020년 4월 7일
편집: KSSV 2020년 4월 7일
iwant = A(A>0)
If you want in the order you have given.
A = A' ;
iwant = A(A>0)

카테고리

Help CenterFile Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by