필터 지우기
필터 지우기

Delete a column from an array of uncertain size?

조회 수: 1 (최근 30일)
Amanda
Amanda 2015년 6월 15일
댓글: Amanda 2015년 6월 15일
Hi everyone.. I know there are answers out there regarding removing a column from an array with x dimensions--this much I have no problem doing.
I'm trying to turn some code into a function that I can use to solve the same type of problem with anywhere from 2 to 7 dimensions (or infinite, if I can write a general enough code).
What I have to do, in a certain part of the code, is take an array of grid points and drop the first point on the second dimension.
E.g. if it was a matrix I would write:
newarray = oldarray(:,2:end);
if it was a 4D array I would write:
newarray = oldarray(:,2:end,:,:);
What do you think the most efficient/general way to code this would be?
Thanks!
  댓글 수: 2
Image Analyst
Image Analyst 2015년 6월 15일
You know in advance what "x" is , right?
Amanda
Amanda 2015년 6월 15일
편집: Amanda 2015년 6월 15일
Yes, but it is determined by the person using the function.
I can, for example, write
if num_dimensions == 2
newarray = oldarray(:,2:end);
elseif num_dimensions == 3
newarray = oldarray(:,2:end,:);
end
but that's not very efficient, I don't think.
My current thought is that I simply create the grid arrays within the function, and drop the first entry of the problematic dimensions before doing so... but this creates some other issues and so I'm curious to know if there is another option.

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

채택된 답변

David Young
David Young 2015년 6월 15일
편집: David Young 2015년 6월 15일
The trick needed is to use the fact that cell arrays can be expanded into comma-separated lists, so can represent any number of subscripts. It works like this.
Test data:
x = 1 + randi(9); % random no. dimensions from 2 to 10
oldarray = rand(repmat(3, 1, x)); % 3 x 3 x 3 ... array
Computation:
% get cell array of subscript arguments representing whole of oldarray
subs = arrayfun(@(s) {1:s}, size(oldarray));
% change second subscript to start from 2
subs{2} = 2:size(oldarray,2);
% create new array by indexing old array
newarray = oldarray(subs{:});
Check newarray is correct size, and look at one column (noting that trailing ones are always OK regardless of the number of dimensions):
disp(size(oldarray));
disp(size(newarray));
disp(oldarray(1, :, 1, 1, 1, 1, 1, 1, 1, 1, 1));
disp(newarray(1, :, 1, 1, 1, 1, 1, 1, 1, 1, 1));
  댓글 수: 1
Amanda
Amanda 2015년 6월 15일
Thanks! This answer and the answer by Walter below both work and both make me so happy. I chose this one at the 'accepted' answer since a test ran in 0.002623 seconds versus 0.014156 seconds.

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2015년 6월 15일
s = size(oldarray);
[r, c, p] = size(oldarray); %deliberate that 3 outputs are given for array that might be more dimensions
newarray = reshape(oldarray,r,c,p);
newarray = reshape(newarray(:,2:end,:), [r, c-1, s(3:end)]);
That is, the 3 dimensional case covers the rest.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by