How to extract elements of each column 1 by 1 in matrix and transform it to a column vector ?
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello everyone!
i want to ask you if it is possible to do what i'm trying to explain or not!
at first i have a binary matrix for example (6*4) as follow :
M=
[1 0 0 0
0 1 0 0
0 1 0 0
0 0 0 1
0 0 0 1
0 0 0 1 ]
in each column i want to verify :
if "1 " is the first element in column (only "1" i don't consider "0") so i try to fill a new column vector V in its first element equal to "0"
if "1" is the second element in column so the second element in V is "k : it is a value"
if "1" is the third element in column so the third element in V is "2*k"
......
as a result i want a vector as shown below
V =
[ 0
0
k
0
k
2*k]
I hope all is clear.
Thanks in advance.
8 commentaires
Dyuman Joshi
le 16 Sep 2022
"In the second row, 1 appears at 1st element of column so the value is 0"
Second row is [0 1 0 0]
Can you explain your statement regarding this - '1 appears at 1st element of column'
What I can observe is, in the 2nd row, 1 appears as the 2nd element.
Réponse acceptée
Dyuman Joshi
le 16 Sep 2022
Apologies, it took quite some time and questions to understand.
Code for the 1st non-zero element in a row -
M=[1 0 0 0
0 1 0 0
0 1 0 0
0 0 0 1
0 0 0 1
0 0 0 1];
v=zeros(size(M,1),1);
for i=1:size(M,1)
c=find(M(i,:),1);
v(i,1)=nnz(M(1:i-1,c));
end
v
You can multiply the vector 'v' with any arbitary value of k.
2 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!