필터 지우기
필터 지우기

How can I dynamically assign/access elements in a matrix with arbitrary dimensions?

조회 수: 10 (최근 30일)
I am creating a code to manipulate a matrix generated from an external file. I would like to make it so that the matrix could have arbitrary dimensions, e.g. in one file it might be just one dimension so listing all the elements could be done like:
A(:)
in another, it might have two dimensions, so listing the elements could be done like:
A(:,:)
if three,
A(:,:,:)
and so on. My problem is that I don’t know how (or maybe it is not possible) to make some general code to manipulate the matrix A. If I had know that A had dimension 3, I could just do something like
A(n,m,o)
But what I would like to do is something like
A(C)
where C is a list equal to [n m o] or [n m o … z].
Any ideas?
~Jensen~

답변 (1개)

Jan
Jan 2013년 1월 15일
편집: Jan 2014년 7월 15일
Not matching the question exactly, but worth to mention:
A = rand(2,3,4) % Or: rand(2,3,4,5,4,3,2)
Index = cell(1, ndims(A));
Index(:) = {':'}; % BUGFIX: was {:}, thanks Walter and Jim
A(Index{:}) % Equivalent to: A(:, :, :)
Index{2} = 2:3;
A(Index{:});
  댓글 수: 6
Chris
Chris 2016년 6월 24일
You made my day! I 've been searching & trying for almost three hours...

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

카테고리

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