How do I break an array into individual element without using loops
조회 수: 3 (최근 30일)
이전 댓글 표시
x=[3,4,5,6,7,8,9,10]
for i=1:length(x)
y=x(i)
end
I want a function to give me this result without using a loop
댓글 수: 5
DGM
2023년 12월 17일
편집: DGM
2023년 12월 17일
Repetition doesn't answer the question. The example you gave is ostensibly the thing you want, but in that example, y is a scalar which changes value with each loop iteration. So if there is no loop, what is the equivalent? I posed two interpretations:
- A single scalar variable which is multivalued (which is nonsense)
- A pile of numbered scalar variables (which are not practically addressable)
If we assume that you mean to create a pile of numbered variables, then there's another question you need to answer. Once you have a hundred numbered variables in memory, how do you intend on addressing them? Spoiler: the solution to the unnecessary problem you're creating is another problem.
As to how you'd create them all those variables, well that would still involve a loop. If you don't want a loop, then you're going to have to write it all out like @Image Analyst pointed out. At that point, if you just intend on writing out every single operation on every single element of every array in your entire workflow, then what's the point in using MATLAB to do it?
답변 (1개)
madhan ravi
2023년 12월 16일
편집: madhan ravi
2023년 12월 16일
y = reshape(x, 1, 1, [])
y(:, :, 1)
This makes it a 3 D array
If you want it as a cell array then
y = num2cell(x)
celldisp(y)
The loop you use is just looping through x vector.
댓글 수: 1
madhan ravi
2023년 12월 16일
편집: madhan ravi
2023년 12월 16일
Perhaps you mean’t:
fprintf('y = %d\n', x(:))
Just for printing values as you do using loop.
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!