A Quickie: do I have to create a temp array in my code

조회 수: 1 (최근 30일)
Scragmore
Scragmore 2011년 11월 28일
Hi,
Q? do I have to create the temp array 'abMt'. Can I rap 'TxTRIn{ii,2}(indx,2:5)' in max to get ans?
1 first I create an index of the section of the array I wish to find max
2 load required data into temp array
3 max(array(:)) returns absolute max for complete data set.
1 indx = (TxTRIn{ii,2}(:,1)>= 800) & (TxTRIn{ii,2}(:,1)<= 1500);
2 abMt = TxTRIn{ii,2}(indx,2:5);
3 abMax = max(abMt(:));
Thanks

채택된 답변

David Young
David Young 2011년 11월 28일
It looks as if TxTRIn{ii,2}(indx,2:5) is 2D. If that's correct, you can write
abMax = max(max(TxTRIn{ii,2}(indx,2:5)));
to avoid the temporary array. However, the way you have it now is probably clearer to read, and I doubt that it is any less efficient.
  댓글 수: 3
Scragmore
Scragmore 2011년 11월 28일
FYI,
looks like I am sticking with the temp array. over 1000 cycles of my function the times came in 21sces for max(max()) and 17secs for max(temp_array(:))
AD
Walter Roberson
Walter Roberson 2011년 11월 28일
And for max(reshape(...)) ?
There isn't much overhead for creating a temporary variable to store something that is already calculated. The overhead comes if you modify the temporary array. Using (:) to reshape to a column vector does not require modifying the temporary array, just creating a header with the dimensions changed but pointing to the same data block.

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2011년 11월 28일
max(TxTRIn{ii,2}(indx,2:5)) is valid syntax, but it would give one maximum per column rather than an overall maximum.
It is not allowed use TxTRIn{ii,2}(indx,2:5)(:)
It is, though, allowed to use reshape(TxTRIn{ii,2}(indx,2:5),[],1)
  댓글 수: 1
Scragmore
Scragmore 2011년 11월 29일
FYI,
max(reshape(...)) came out consistently at 21secs.
max(temp_array(:)) it is then.

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

카테고리

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