How do I break an array into individual element without using loops

x=[3,4,5,6,7,8,9,10]
x = 1×8
3 4 5 6 7 8 9 10
for i=1:length(x)
y=x(i)
end
y = 3
y = 4
y = 5
y = 6
y = 7
y = 8
y = 9
y = 10
I want a function to give me this result without using a loop

5 Comments

What result? Do you want a single scalar numeric variable to simultaneously be multivalued? ... or do you want to create a pile of unmanageable numbered variables? One of these is nonsensical, and the other is just an incredibly bad idea.
I wanted the elements piece by piece. Thanks
That still makes no sense to us. Did you see @madhan ravi's answer below?
Please make up the variables that you'd like to have, save them into a .mat file, and attach the mat file with the paperclip icon. Then we can try to figure out how to create those variables from your input "x" variable.
Why do you want to do this quirky thing anyway? Why do you think it is necessary or convenient?
For the specific example you have, the only way I see to do it is:
x=[3,4,5,6,7,8,9,10]
y1 = x(1);
y2 = x(2);
y3 = x(3);
y4 = x(4);
y5 = x(5);
y6 = x(6);
y7 = x(7);
y8 = x(8);
but we still don't recommend it. It's much better to just reference the element of x by its index rather than some uniquely named other variable.
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:
  1. A single scalar variable which is multivalued (which is nonsense)
  2. 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?
@madhan ravi @Image Analyst @DGM Thank you all for your contribution.
Madhan ravi gave me the closest to what I thought I want but I have learnt that it doesn't make sense. I'll keep learning and improving.
Thank you.

Sign in to comment.

Answers (1)

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 Comment

Perhaps you mean’t:
fprintf('y = %d\n', x(:))
Just for printing values as you do using loop.

Sign in to comment.

Products

Asked:

on 16 Dec 2023

Commented:

on 20 Dec 2023

Community Treasure Hunt

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

Start Hunting!