필터 지우기
필터 지우기

How to extract elements of each column 1 by 1 in matrix and transform it to a column vector ?

조회 수: 1 (최근 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
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.
Safia
Safia 2022년 9월 16일
"What I can observe is, in the 2nd row, 1 appears as the 2nd element".
in the 2nd element in the row, but when i see the position of "1" in column , it the first non zero element right?

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

채택된 답변

Dyuman Joshi
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
v = 6×1
0 0 1 0 1 2
You can multiply the vector 'v' with any arbitary value of k.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by