Multiple element at same index

조회 수: 2 (최근 30일)
Ozge Moral
Ozge Moral 2016년 7월 19일
답변: Ozge Moral 2016년 7월 19일
I have a question about storing multiple element at same index.
x = [-4 -2 0 2 4];
After some conditions,elements will change their places and some elements will be in same index. I want to create a vector/array/structure like this.
for example : first element to second index, Second element to third index, Third,fourth and fifth element to fourth index:
y = [0 -4 -2 (0,2,4) 0];
But i used arrays, just fifth element is at fourth index. What kind of structure can i use? i'm confused how i store multiple elements at same index.
If it is complicated to understand, i can write all code. I searched posts but i didn't find approximate solution. Thank you.

답변 (3개)

Adam
Adam 2016년 7월 19일
편집: Adam 2016년 7월 19일
Use a cell array if you really want to do this i.e.
y = { 0, -4, -2, [0 2 4], 0 };
Be aware though that using cell arrays for numeric operations considerably slows down your code so you should think about if this is the best way to design what you are doing if speed is important to you.

Guillaume
Guillaume 2016년 7월 19일
Use a cell array:
y = {0, -4, -2, [0, 2, 4], 0} %cell array creating
y{1} %access element(s) at 1st index
y{3} %access element(s) at 3rd index

Ozge Moral
Ozge Moral 2016년 7월 19일
Thank you for your responses. I will try to use cell array in my code. But it is little complicated, i will add my whole code if i don't do.

카테고리

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