Call one array from a large array made up of several others

1 Ansicht (letzte 30 Tage)
Jennifer
Jennifer am 5 Dez. 2013
Kommentiert: Matt J am 5 Dez. 2013
I've got an array, say Z, made up of 4 smaller 2x2 ones, say A, B, C and D:
Z = a11 a12 b11 b12
a21 a22 b21 b22
c11 c12 d11 d12
c21 c22 d21 d22
Once I've created Z, is there a way to ask matlab for, for example, the first entry in each of A, B, C and D, based only on Z? I can call individual entries from it fine (e.g. the value at the 4th row, 3rd column), but would like to be able to store my data in one array and still treat it as individual arrays for some things.

Akzeptierte Antwort

sixwwwwww
sixwwwwww am 5 Dez. 2013
you can do it fi you store your arrays A, B, C and D in cell array Z as follows:
Z = {A, B, C, D};
then you can treat A, B, C and D individually by using cell indexing
  3 Kommentare
sixwwwwww
sixwwwwww am 5 Dez. 2013
here is the way you can do it:
% Your matrices A, B, C and D
A = ones(2);
B = A;
C = A;
D = A;
% Cell array of your matrices
Z = {A, B, C, D};
% Now if you want to access A and want to replace its each element with 2 then you can do it as follow
Z{1}(:) = 2;
% Now if you want to access B and want replace its first element with 3 then you can do it as follow
Z{2}(1, 1) = 3;
% Now you can see the changes you made to A and B
disp(Z{1})
disp(Z{2})
In the same way you can easily you use matrices A, B, C and D by using proper cell indexing as described above
Jennifer
Jennifer am 5 Dez. 2013
Thank you! I hadn't come across the disp(Z{1}) part before, it works now

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Matt J
Matt J am 5 Dez. 2013
Bearbeitet: Matt J am 5 Dez. 2013
Z([1,3],[1,3])
  2 Kommentare
Jennifer
Jennifer am 5 Dez. 2013
Hi - thanks for the quick answer! I haven't been able to get your suggestion to work, here's what I tried:
A = [1 2; 1 2];
B = [3 3; 4 4];
C = [9 8; 7 6];
D = [2 4; 6 8];
Z = {A B; C D}
Z([1,2],[1,2])
and that gives:
ans =
[2x2 double] [2x2 double]
[2x2 double] [2x2 double]
Have I misunderstood a part of this?
Matt J
Matt J am 5 Dez. 2013
Here are some examples,
>> Z=10*reshape(1:16,4,4)'
Z =
10 20 30 40
50 60 70 80
90 100 110 120
130 140 150 160
>> Z([1,3],[1,3])
ans =
10 30
90 110
>> Z([2,4],[2,3])
ans =
60 70
140 150

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by