필터 지우기
필터 지우기

Choosing Specific Elements from An Array

조회 수: 22 (최근 30일)
Pranjal Pathak
Pranjal Pathak 2013년 11월 3일
편집: Andrei Bobrov 2013년 11월 3일
Hi,
My problem is:
I have a matrix A. From it, I want to choose only some of the specific elements and place them in another array B at the specified locations. But, by default it is becoming of the same dimension as A, with zero at those locations. How to get only the non-zero elements at the specified locations with no zeros?
A=[1 2 3 4 5;
6 7 8 9 10;
11 12 13 14 15;
16 17 18 91 20;
21 22 23 24 25];
B(1,3)=A(1,3);
B(2,2:4)=A(2,2:4);
B(3,1:5)=A(3,1:5);
B(4,2:4)=A(4,2:4);
B(5,3)=A(5,3);
Thanking You!

채택된 답변

Andrei Bobrov
Andrei Bobrov 2013년 11월 3일
편집: Andrei Bobrov 2013년 11월 3일
[ii,jj] = ndgrid(1:5);
k = abs(ii-jj)<=2;
B = A.*(k&fliplr(k));
ADD
A1 = A';
B = A1(k&fliplr(k));

추가 답변 (2개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 11월 3일
B=[A(1,3) A(2,2:4) A(3,2:5) A(4,2:4) A(5,3)]

Image Analyst
Image Analyst 2013년 11월 3일
Try this:
out = A(B~=0)
  댓글 수: 1
Azzi Abdelmalek
Azzi Abdelmalek 2013년 11월 3일
If A(5,3) is equal to 0,this will not work

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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by