필터 지우기
필터 지우기

vectorization - problem with zeros

조회 수: 2 (최근 30일)
Julius
Julius 2012년 6월 23일
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
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.

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

채택된 답변

Julius
Julius 2012년 6월 23일
  • Thank you Monkey, I really appreciate your help, but it seems that I've found a more effective solution:*
A=[1 3 2 4;
5 6 7 8;
9 1 2 3;
4 5 6 2];
R=[1 3 0 7];
B=zeros(7);
RNZ=R(R ~= 0);
d=find(R);
T=A(d,d');
B(RNZ,RNZ')=T

추가 답변 (1개)

Luffy
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

카테고리

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