Is there a function where the inputs are indices and the outputs are vectors with a value of 1 of that index and the remainder?

조회 수: 1 (최근 30일)
I am finding a function (named func, here).
If I type the following code
y = func(10, [3, 5, 1]);
then I get the following result
y = [1; 0; 1; 0; 1; 0; 0; 0; 0; 0];
The vector y is a vector with size of 10 by 1, in which the third, fifth, and first elements are one, but the rest are all zero.
Actually, I am using now the following code:
a = [3, 5, 1];
y = zeros(10,1);
y(a) = 1;
I want to use an internal function!!
  댓글 수: 1
Walter Roberson
Walter Roberson 2020년 2월 20일
You can construct expressions that create it without an explicit temporary variable, but they will be longer and less efficient than what you have now.

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

답변 (1개)

Siriniharika Katukam
Siriniharika Katukam 2020년 2월 25일
The procedure you followed seems to be a better way of doing it.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by