필터 지우기
필터 지우기

What does this array operation do?

조회 수: 10 (최근 30일)
Eric
Eric 2022년 2월 8일
댓글: Eric 2022년 2월 8일
I have two 2D square arrays of the same size, array1 and array 2. What does this operation do?
x = array1(array2)

채택된 답변

Image Analyst
Image Analyst 2022년 2월 8일
If array2 is a logical (binary) array then it takes those elements from array1 that are 1 in array2.
For example
array1 = magic(5)
array1 = 5×5
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
array2 = false(5);
array2(2, 3) = true;
array2(3, 1) = true
array2 = 5×5 logical array
0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
x = array1(array2) % This will be a vector, not a 2-D array.
x = 2×1
4 7
So see how it took the (3,1) and (2,3) element and strung them together into a vector?
On the other hand if the array2 is an integer array, it will use those as linear indexes into the array 1.
The linear indexes are in column major order (down rows first, them move across columns):
1 6 11 16 21
2 7 12 17 22
3 8 13 18 23
4 9 14 19 24
5 10 15 20 25
So if array 2 was
array2 = [2, 7; 17,22]
array2 = 2×2
2 7 17 22
x = array1(array2)
x = 2×2
23 5 14 16
It will take those elements from row 2 that had those linear indexes.
  댓글 수: 1
Eric
Eric 2022년 2월 8일
Thank you. That answers my question!

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

추가 답변 (1개)

KSSV
KSSV 2022년 2월 8일
  1. If array2 is double and have fractions it will throw error.
  2. If array2 is logical i.e. it has 0 and 1's. It will give elements of array1 where array2 is 1.
  3. If array2 has all integers, it will give the respective elements. As array2 acts like indecing.
  댓글 수: 1
Eric
Eric 2022년 2월 8일
Thank you. That answers my question.

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

카테고리

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

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by