필터 지우기
필터 지우기

how to extend third dimension of matrix?

조회 수: 18 (최근 30일)
SONI SONI
SONI SONI 2015년 2월 19일
댓글: Stephen23 2015년 2월 20일
If I have a three dimensional matrix 180*360*5, suppose third dimension representing 5 months of a year (feb, march, may, aug, dec) third. I need to extend its third dimension and result should be 180*360*12. Now would I insert missing months. then I also want to assign zero on inserted months.

채택된 답변

the cyclist
the cyclist 2015년 2월 19일
If A is your array:
A = cat(3,A,zeros(180,360,7));
  댓글 수: 2
SONI SONI
SONI SONI 2015년 2월 20일
I understand but this code is extending only the length of array at right sight. as I mentioned I want to insert missing months(jan, apr.....). Sequence of month is discontinue(feb, march, may, aug, dec) . So I want to create a continue series (jan, feb, mar, apr, may, june, jul, aug, sep, oct, nov, dec). please suggest me how should I solve this problem?
Stephen23
Stephen23 2015년 2월 20일
편집: Stephen23 2015년 2월 20일
This is not clear at all. You need to explain your requirements to us clearly. Your question stated:
  • "result should be 180*360*12 180*360*12"
  • "assign zero on inserted months"
Both The Cyclist's and my own answer provide you exactly this. Can you see how we tried to help you by giving you what you asked for ? If you want something else, then you will have to explain it in more detail, with examples. Sorry, but we don't read minds.

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

추가 답변 (1개)

Stephen23
Stephen23 2015년 2월 19일
편집: Stephen23 2015년 2월 19일
The fastest way to enlarge an existing array is to simply assign a value in one "corner" of your array:
A = [180x360x5] % define your array...
A(1,1,12) = 0;
This places the given value (zero) and fills the undefined values with zero. The final dimensions of this example will be 180x360x12. This method has numerous advantages over other methods (such as concatenation):
  • very fast!
  • no creation of intermediate variables
  • even though the zero is class double, because it is a scalar double it will be cast to match the class of A (uint, int, and even character).
  • by specifying the first indices as ones (1,1,...) it is robust to any changes in the array size, without rewriting.
  댓글 수: 1
Stephen23
Stephen23 2015년 2월 20일
Second attempt based on your comment:
B(180,360,12) = 0; % create array.
B(:,:,[2,3,5,8,12]) = A

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

카테고리

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