How to extract elements of each column 1 by 1 in matrix and transform it to a column vector ?
조회 수: 2 (최근 30일)
이전 댓글 표시
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
Dyuman Joshi
2022년 9월 16일
"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.
채택된 답변
Dyuman Joshi
2022년 9월 16일
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!