Matrix split in vector

1 Ansicht (letzte 30 Tage)
steve Brian
steve Brian am 7 Apr. 2020
Kommentiert: Stephen23 am 7 Apr. 2020
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]

Akzeptierte Antwort

Stephen23
Stephen23 am 7 Apr. 2020
Bearbeitet: Stephen23 am 7 Apr. 2020
>> B = nonzeros(A.')
B =
1
2
3
4
5
6
7
8
  2 Kommentare
steve Brian
steve Brian am 7 Apr. 2020
Bearbeitet: steve Brian am 7 Apr. 2020
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 am 7 Apr. 2020
"and if i want B to be in row?"
nonzeros(A.').'

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

KSSV
KSSV am 7 Apr. 2020
Bearbeitet: KSSV am 7 Apr. 2020
iwant = A(A>0)
If you want in the order you have given.
A = A' ;
iwant = A(A>0)

Kategorien

Mehr zu Operating on Diagonal Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by