Is this a valid expression?

조회 수: 3 (최근 30일)
Salad Box
Salad Box 2020년 1월 8일
댓글: Guillaume 2020년 1월 8일
Hi
offsets = [32 48 64 80 96 112];
[X,Y,Z] = ndgrid([-1, 0, 1]);
offsetArray = [Z(:), Y(:), X(:)];
offsetArray(sum( offsetArray == [0, 0, 0], 2 ) == 3, :) = [];
I'm wondering whether below is a valid expression since I see a red tilde warning sign below the left bracket after the transpose sign in below
tmpOffsets = repmat( offsets(:), [1, size( offsetArray, 1 )] )'(:);
And the error message says
tmpOffsets = repmat( offsets(:), [1, size( offsetArray, 1 )] )'(:);
Error: Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
What could possibly went wrong?

채택된 답변

Rik
Rik 2020년 1월 8일
You can't index the output of the transpose function like that. You can either do it explicitly in multiple steps, or use the function syntax:
%option 1:
tmpOffsets = repmat( offsets(:), [1, size( offsetArray, 1 )] );
tmpOffsets = tmpOffsets';
tmpOffsets = tmpOffsets(:);
%option 2:
tmpOffsets= reshape(permute(repmat( offsets(:), [1, size( offsetArray, 1 )] ),[2 1]),[],1);
I would go for option 1, because it is much easier to see what is happening.
  댓글 수: 1
Guillaume
Guillaume 2020년 1월 8일
Note: for the record, option 3 is simpler and even easier to see what is happening:
tmpOffsets = repelem(offsets(:), size(offsetArray, 1));
should be marginally faster as well.

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

추가 답변 (1개)

Guillaume
Guillaume 2020년 1월 8일
편집: Guillaume 2020년 1월 8일
As matlab tells you is not valid, you can't have (:) on the output of a function. Since (:) is simple a reshape,
tmpOffsets = reshape(repmat( offsets(:), [1, size( offsetArray, 1 )] )', [], 1);
would work.
edit: on the other hand, the above is simply:
tmpOffsets = repelem(offsets(:), size(offsetArray, 1));
  댓글 수: 1
Salad Box
Salad Box 2020년 1월 8일
Many thanks! Your solutions are also perfect for this task!

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

카테고리

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