Indexing of fresh array in one line without intermediate variable.
Ältere Kommentare anzeigen
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)
EDIT2. Finally found the answer here: http://stackoverflow.com/questions/3627107/how-can-i-index-a-matlab-array-returned-by-a-function-without-first-assigning-it
4 Kommentare
Azzi Abdelmalek
am 6 Jun. 2013
Give an example
Maxim
am 6 Jun. 2013
UniqueWorldline
am 14 Nov. 2017
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.
"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)
If you really want the number of columns only then the simple and reliable approach is this:
size(A,2)
Akzeptierte Antwort
Weitere Antworten (2)
Azzi Abdelmalek
am 6 Jun. 2013
Bearbeitet: Azzi Abdelmalek
am 6 Jun. 2013
1 Kommentar
Maxim
am 6 Jun. 2013
DH
am 28 Dez. 2023
Hope this helps.
interp1(Func(1:10), 2:3)
Kategorien
Mehr zu Matrix Indexing finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!