Extracting parts of a matrix implicitly (without storing the matrix)
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi,
Suppose I have a matrix [1 1 1; 2 2 2; 3 3 3] and suppose that I want to extract its second row. Of course I could do:
temp=[1 1 1; 2 2 2; 3 3 3];
temp(2,:)
But what if I don't want to store the matrix temporarily in temp? If I try the following:
[1 1 1; 2 2 2; 3 3 3](2,:)
I get an error message: "Error: Unbalanced or unexpected parenthesis or bracket." Is there a way to accomplish referring to a matrix implicitly (that is, without storing it or giving it a name)? Will this be faster than first storing the matrix in the variable temp? (I only want to do this because I speculate it may be faster.)
Thank you very much,
Andrew DeYoung
Carnegie Mellon University
댓글 수: 1
Matt Fig
2011년 5월 21일
There may be ways to avoid creating the temp matrix programmatically, depending on what you are doing. If you show more code we might be able to make a suggestion.
채택된 답변
Walter Roberson
2011년 5월 20일
The time to store it in a temporary matrix will be negligible: it still has to create all the data structures for the variable except for the actual entry in the name tables.
Anyhow, you could try timing the following but I don't think you'll be overjoyed with the speed improvement:
subsref([1 1 1; 2 2 2; 3 3 3], struct('type', '()', 'subs', {{2, ':'}}))
댓글 수: 1
Walter Roberson
2011년 5월 21일
You can also go through the trouble of:
rowN = @(A,N) A(N,:);
rowN([1 1 1;2 2 2; 3 3 3], 2)
but I think you will find this to be slower than a temporary variable.
추가 답변 (1개)
James Tursa
2011년 5월 21일
You can do what you show directly in Fortran, and kinda do it in C, but MATLAB has no official way of pointing to parts of other arrays. You will need to make the temp copy, or as Matt suggests, show more code to see if there is a way to do what you are attempting without making the temp copy. There is an unofficial way of pointing to the columns of a matrix (i.e. contiguous data) by Bruno Luong, but misuse can bomb MATLAB. You can find it here:
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!