How to access a vector of a 3D array
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I have a code :
%%%%%%%%%%%%%%%%%%%%%%%%
for i = 1 : 3
for j = 1 : 5
P{i,j} = rand(1,2);
end
end
P{1,:}(2)
% I want to use this to access a certain vector from this 3D matrix P
% But matlab told me "Bad cell reference operation". Can anyone tell me
% the reason and how to do it? Thanks a lot!
0 Kommentare
Akzeptierte Antwort
Jan
am 20 Dez. 2012
Using a cell array is an inefficient representation. Better use a 3D array:
P = zeros(3, 5, 2);
for i = 1 : 3
for j = 1 : 5
P(i,j,:) = rand(1,2);
end
end
Now the indexing is trivial:
P(1, :, 2)
Weitere Antworten (2)
Walter Roberson
am 19 Dez. 2012
P is not a 3D matrix. You could convert it to a 3D matrix by using cell2mat().
The P that you have established cannot be referenced the way you want with a simple operation. Consider using cellfun
cellfun(@(V) V(2), P)
2 Kommentare
Walter Roberson
am 21 Dez. 2012
The V does not mean anything to cellfun. The V is part of the anonymous function, @(V) V(2) that is being used. That means "This is an anonymous function that accepts a single parameter that we will temporarily refer to as "V"; what we should do when this function is called is to return the second element of the array currently named "V", which is to say the second element of whatever was passed in as the first parameter of the anonymous function."
Siehe auch
Kategorien
Mehr zu Cell Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!