What is the meaning of A (: ,: ,1) in matlab? where A is a matrix

조회 수: 368 (최근 30일)
bh dhouha
bh dhouha 2015년 2월 16일
편집: Alexander Vassilev 2022년 10월 27일
What is the meaning of A (: ,: ,1) in matlab? where A is a matrix .thanks

채택된 답변

Image Analyst
Image Analyst 2015년 2월 16일
If the badly-named "A" represents a color image, then A(:, :, 1) would mean all rows and all columns in the first image plane - in other words, the red channel of the image. The code mean(mean(A(:,:,1))) would mean the average intensity of the red channel:
meanRed = mean(mean(A(:,:,1)));
Stephen and I are guessing because the author just used a single letter of the variable rather than some descriptive name would would give us some context as to what "A" represents. I hate code that just looks like a random alphabet soup of single letter variables - it makes it very hard to understand, follow, and maintain. For example if the variable were named rgbImage, then we'd know what "A" represented. As of now, we don't. You didn't supply any context either so we're left to speculate (or just ignore the question which is probably what most people did).
  댓글 수: 3
Walter Roberson
Walter Roberson 2016년 1월 27일
That response is not rude: it makes educated guesses about the real meaning of the question, and then it describes the difficulties with how the question was phrased, offering alternatives.
Any code that is intended to last should be written with the thought that someone else may have to read it, so variable names should be self-explanatory or follow conventions that shape the meaning (e.g., 'x' and 'y' might not really explain what the data is for, but the convention is well enough ingrained that it is easy to follow.)
Alexander Vassilev
Alexander Vassilev 2022년 10월 27일
편집: Alexander Vassilev 2022년 10월 27일
I was bumped into the same question, and I would agree with Mr. Hodren. The answer that followed surprized me because the tone and the manner it is formulated in does not adjust with the respectable community.

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

추가 답변 (1개)

Stephen23
Stephen23 2015년 2월 16일
편집: Stephen23 2015년 2월 16일
A(:,:,1) means: all rows and all columns of A that are in its first page.
(The third dimension is referred to in the MATLAB documentation as a "page", just as the first dimension is "row" and the second is "column").
In MATLAB all arrays can be multidimensional, and the contents can be referred to using indexing . In your example the variable A has three dimensions, and they are referred to in this way:
A(:, % all rows
:, % all columns
1) % on the first page
You should read about the colon operator too.
Although you write that A is a matrix, actually it might not be: a matrix (by definition) only has two dimensions (all higher dimensions are one), which means that if the third dimension has size two or more, then it will be an array, rather than a matrix. You might like to review matrix indexing as well.
  댓글 수: 3
Stephen23
Stephen23 2015년 2월 16일
편집: Stephen23 2015년 2월 16일
That depends on the size of A. The behavior of mean changes depending on the size of the array that is passed to it, so you will have to tell us size(A) before we can answer that question.
If you want the mean of all elements of an array, regardless of its size or number of dimensions, then you can use mean(A(:)).
Note that mean also has an optional argument that specifies which dimensions it calculates along. We can use this for example, if A has three dimensions, to get the mean of each page using the following code:
mean(mean(A,1),2)
bh dhouha
bh dhouha 2015년 2월 16일
Thanks a lot Stephen :)

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by