Indexing of fresh array in one line without intermediate variable.

조회 수: 99 (최근 30일)
Maxim
Maxim 2013년 6월 6일
답변: DH 2023년 12월 28일
How do I index a newly made array without intermediate variables?
Maybe not the best example, but imagine I want to do this
regexp(string,expression,'match')(10:20)
How do I do it in one line?
It is not a regexp question. This problem occurs in many different situations. Thank you
EDIT Another example
function ans = Func(array)
array
end
I want it to look like
Func(1:10)(2:3)
But I need to do
temporary = Func(1:10);
temporary(2:3)
  댓글 수: 4
UniqueWorldline
UniqueWorldline 2017년 11월 14일
For anyone finding this question as late as I have, but who is looking for a simple solution for the
size
function, do the following to only get the one index of the array size you are looking for:
A = [1,2,3,4,5]; % Some array
arrayCols = size(A); % This returns a 1x2 array with the number of rows
% and columns of A. Which is undesirable if only
% the columns are wanted. Instead do this:
[arrayRows,arrayCols] = size(A);
Now arrayCols is a scalar value that can be used immediately. This doesn't allow you to do
size(A)(2)
but it can still eliminate an intermediate.
Stephen23
Stephen23 2023년 11월 9일
편집: Stephen23 2023년 11월 9일
"This returns a 1x2 array with the number of rows and columns of A."
No. Strictly speaking that actually returns the combined size of all dimensions >=2. Lets try it now:
A = rand(5,4,3);
[rows,notcolumns] = size(A)
rows = 5
notcolumns = 12
If you really want the number of columns only then the simple and reliable approach is this:
size(A,2)
ans = 4

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

채택된 답변

Maxim
Maxim 2013년 6월 6일
편집: Maxim 2013년 6월 6일
Problem
magic(5)(3)
Solution 1
value = subsref(magic(5),struct('type','()','subs',{{3,3}}));
Solution 2
paren = @(x, varargin) x(varargin{:});
paren(magic(5), 3, 3);
  댓글 수: 2
Jose Daniel Ortiz
Jose Daniel Ortiz 2018년 9월 18일
So, basically, Matlab does not really support execution and indexing in the same line. This is in contrast to python, in which you can have ludicrously and arbitrarily complex chains of computation in a single line such as:
thing = A(B(size(A)(0))(0))
Walter Roberson
Walter Roberson 2020년 2월 6일
MATLAB does "really" support execution and indexing in the same line, but the syntax for it is pretty ugly. And you need a real assignment in order to capture any output after the first output from a function that returns multiple outputs.

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

추가 답변 (2개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 6월 6일
편집: Azzi Abdelmalek 2013년 6월 6일
Look at
Example
a=[2 3 5 8]
out=arrayfun(@(x) x^2+sin(x),a)
  댓글 수: 1
Maxim
Maxim 2013년 6월 6일
I know these functions, but don't see how they could be helpful. The problem is that I can't index the fresh array.

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


DH
DH 2023년 12월 28일
Hope this helps.
interp1(Func(1:10), 2:3)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by