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!