How to extract elements of each column 1 by 1 in matrix and transform it to a column vector ?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Safia
el 16 de Sept. de 2022
Comentada: Dyuman Joshi
el 17 de Sept. de 2022
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 comentarios
Dyuman Joshi
el 16 de Sept. de 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.
Respuesta aceptada
Dyuman Joshi
el 16 de Sept. de 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 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!