Suppose I have 1 by 2N vector
x=zeros(1,2*N);
Now I have N of 1 by 2 vector
y1=ones(1,2), y2=ones(1,2)
and I want to fill x as
[y1 y2 ...]
what will be a compact way to fill x in this way (instead of concatenation)?

댓글 수: 1

Stephen23
Stephen23 2018년 4월 2일
Do NOT do this! The MATLAB documentation specifically advises against what you are trying to do: "A frequent use of the eval function is to create sets of variables such as A1, A2, ..., An, but this approach does not use the array processing power of MATLAB and is not recommended. The preferred method is to store related data in a single array"
Read this to know more:

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

답변 (1개)

Walter Roberson
Walter Roberson 2018년 3월 27일

0 개 추천

x(1:2:end) = y1;
x(2:2:end) = y2;
or
x = reshape([y1; y2], 1, []); %requires that y1 and y2 be row vectors
or
x = reshape([y1(:), y2(:)].', 1, []); %does not requires that they be row vectors

댓글 수: 3

alpedhuez
alpedhuez 2018년 3월 27일
편집: alpedhuez 2018년 3월 27일
But in reality, N is large and there are many y1, y2,...., y100 so that it cannot be brute force. Then what needs to be done?
Walter Roberson
Walter Roberson 2018년 3월 28일
편집: Walter Roberson 2018년 3월 28일

http://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval

Though I see now that I misinterpreted your question -- I thought you wanted alternating elements, but instead you were looking for dynamic variable names.

alpedhuez
alpedhuez 2018년 3월 28일
Let me think about it and phrase questions in a clearer way. Thank you.

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

카테고리

도움말 센터File Exchange에서 Just for fun에 대해 자세히 알아보기

제품

태그

질문:

2018년 3월 27일

댓글:

2018년 4월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by