From: Antonio Trujillo-Ortiz on
Hi all,

I need your on this. Suppose I have the cells indicators 1,2,3,4; such as
a{1},a{2},a{3},a{4} .
How can I replace them with the new ones eg. 7,9,10,14, in order to have
a{7},a{9},a{10},a{14} ?

I'll apreciate any hint.

Thx
From: us on
"Antonio Trujillo-Ortiz" <atrujo(a)uabc.edu.mx> wrote in message <i39idk$sb4$1(a)fred.mathworks.com>...
> Hi all,
>
> I need your on this. Suppose I have the cells indicators 1,2,3,4; such as
> a{1},a{2},a{3},a{4} .
> How can I replace them with the new ones eg. 7,9,10,14, in order to have
> a{7},a{9},a{10},a{14} ?
>
> I'll apreciate any hint.
>
> Thx

one of the solutions

a={1,magic(2),magic(3),magic(4)};
ix=1:3;
a{ix}
% not shown...
ix=[3,2,4];
r={a{ix}}
% r = [3x3 double] [2x2 double] [4x4 double]

us
From: Antonio Trujillo-Ortiz on
Hi us,

....Thanks for your answer. However, as you can see, my new linear indexing code [7,9,10,14] is out of the old ones [1,2,3,4]. For I got the message,

??? Index exceeds matrix dimensions.

Antonio
From: Oleg Komarov on
"Antonio Trujillo-Ortiz" <atrujo(a)uabc.edu.mx> wrote in message <i39lqc$8ho$1(a)fred.mathworks.com>...
> Hi us,
>
> ...Thanks for your answer. However, as you can see, my new linear indexing code [7,9,10,14] is out of the old ones [1,2,3,4]. For I got the message,
>
> ??? Index exceeds matrix dimensions.
>
> Antonio

a = num2cell(rand(1,14));
a(1:4) = a([7,9,10,14])

which in fact is what us proposed but from a different point of view.

Oleg
From: Walter Roberson on
Antonio Trujillo-Ortiz wrote:

> I need your on this. Suppose I have the cells indicators 1,2,3,4; such as
> a{1},a{2},a{3},a{4} .
> How can I replace them with the new ones eg. 7,9,10,14, in order to have
> a{7},a{9},a{10},a{14} ?

The 1, 2, 3, and so on are not arbitrary numeric keys that are "somehow"
associated with contents (e.g. through some kind of hash table or look up
table): the numbers are array indices like any other kind of array indices. If
you have an a{7} then you also have to have a{1}, a{2}, a{3}, a{4}, a{5}, and
a{6} (though each of those might be empty): it is not possible to have an
array that just has a{7}, a{9}, a{10}, and a{14} without elements 1, 2, 3, 4,
5, 6, 8, 11, 12, and 13 being there (but possibly empty).


If your question is how you could _copy_ a{1}, a{2}, a{3}, a{4} to a{7}, a{9},
a{10}, and a{14} respectively, then you could do that with

a([7 9 10 14]) = a([1 2 3 4]);

Notice that () was used here instead of {}

Note that if you do this, then all of the right-hand-side values are retrieved
before any assignment starts happening, so if one of the indices on the left
hand side happens to be the same as one of the indices on the right hand side,
you do not have to worry that that location might get overwritten before its
value is retrieved; this is good.


If your question is how you could _move_ a{1}, a{2}, a{3}, a{4} to a{7}, a{9},
a{10} and a{14} respectively, with empty arrays being left behold to hold the
positions open, then the simple solution but sometimes wrong solution is

a([7 9 10 14]) = a([1 2 3 4]);
a([1 2 3 4]) = {};

The above solution will only work if none of the indices appear on both sides.
If some of the indices might appear in both places, then you need to do the
equivalent of:

a([7 9 10 14]) = a([1 2 3 4]);
a(setdiff([1 2 3 4], [7 9 10 14])) = {};