How do I assign 3D variables when third dimension has size one?
1 view (last 30 days)
Show older comments
Is this a bug? If not, how am I supposed to code this so dd1 has shape [2 3 1]?
>> sd1 = reshape(1:12,[4,3,1])
sd1 =
1 5 9
2 6 10
3 7 11
4 8 12
>> sd2 = reshape(1:24,[4,3,2])
sd2(:,:,1) =
1 5 9
2 6 10
3 7 11
4 8 12
sd2(:,:,2) =
13 17 21
14 18 22
15 19 23
16 20 24
>> for k = 1:2, dd1(k,:,:) = sd1(2*k,:,:); end
>> for k = 1:2, dd2(k,:,:) = sd2(2*k,:,:); end
>> size(dd1)
ans =
2 1 3
>> size(dd2)
ans =
2 3 2
1 Comment
Accepted Answer
Matt J
on 10 Nov 2014
dd1=sd1(2:2:end,:,:)
3 Comments
Matt J
on 11 Nov 2014
Edited: Matt J
on 11 Nov 2014
I doubt it's a bug. If dd1 and dd2 are not pre-defined, then note that your for-loop results are consistent with the "shiftdim rule" that I describe in your other thread.
The bottom line - it's just one more reason why its dangerous to define or modify the size/shape of an array through assignment.
More Answers (0)
See Also
Categories
Find more on Function Creation in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!