vectorization - problem with zeros
2 ビュー (過去 30 日間)
古いコメントを表示
I need to expand a small matrix A into a bigger one according to a pattern defined by a an array R. But the array may contain zeros so as to know that the rows and columns marked''0'' should be omitted. Here are the matrices:
A=[1 3 2 4; R=[1;
5 6 7 8; 3;
9 1 2 3; 0;
4 5 6 2]; 7];
R'=[1 3 0 7];
% ... the resulting matrix would be:
B=[1 0 3 0 0 0 4;
0 0 0 0 0 0 0;
5 0 6 0 0 0 8;
0 0 0 0 0 0 0;
0 0 0 0 0 0 0;
0 0 0 0 0 0 0;
4 0 5 0 0 0 2];
B=zeros(7,7); B(R,R')=A ......returns an error
1 件のコメント
Luffy
2012 年 6 月 23 日
I do not think you can write B(R,R') as Subscript indices must either be real positive integers or logicals.
採用された回答
その他の回答 (1 件)
Luffy
2012 年 6 月 23 日
A(find(R==0),:)=0; % on doing this u make a row of A=0 at which R=0 assuming there is only 1 zero in R as per you example otherwise add a for loop.
A = [1 3 2 4;
5 6 7 8;
0 0 0 0;
4 5 6 2];
A(:,find(R'==0))=0; % on doing this u make a column of A=0 at which R'=0 assuming there is only 1 zero in R
A = [1 3 0 4;
5 6 0 8;
0 0 0 0;
4 5 0 2];
B = zeros(7); % to create bigger matrix B,i took it as per your example as 7X7 matrix,
B = [ 0 0 0 0 0 0 0;
0 0 0 0 0 0 0;
0 0 0 0 0 0 0;
0 0 0 0 0 0 0;
0 0 0 0 0 0 0;
0 0 0 0 0 0 0;
0 0 0 0 0 0 0];
j = 1;
for i =1:2:length(B(1,:))
l = 1;
for k = 1:2:length(B(:,1))
B(i,k) = A(j,l);
l = l+1;
end
j = j+1;
end
B;
B = [ 1 0 3 0 0 0 4;
0 0 0 0 0 0 0;
5 0 6 0 0 0 8;
0 0 0 0 0 0 0;
0 0 0 0 0 0 0;
0 0 0 0 0 0 0;
4 0 5 0 0 0 2];
% this should give you answer you are looking for
% I am not generally good at following coding practices,if u do not understand any part feel free to comment
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!