I'm working on an example in the fundamental manual. I'm note sure if I understand what is happening.
Create a cell array
C = {'one','two','three';1,2,3},
{'one'} {'two'} {'three'}
{[ 1]} {[ 2]} {[ 3]}
Create a subset of the cell array
upperLeft = C(1:2,1:2)
{'one'} {'two'}
{[ 1]} {[ 2]}
I tried to do as above in creating the subset. I was thinking the code below should copy the cell array but it gives an error. I'm not sure I'm understanding what is going on when I do this.
CopyArray = C(1:2,1:2,1:2)
Any help will be appreciated,
Thanks
D

 채택된 답변

James Tursa
James Tursa 2020년 4월 28일
편집: James Tursa 2020년 4월 28일

0 개 추천

The variable C is only a 2D variable having two dimensions. You have requested indexing into a third dimension
with that last 1:2, hence the error. Similar to doing this:
>> M = [1 2 3;4 5 6]
M =
1 2 3
4 5 6
>> M(2,3)
ans =
6
>> M(2,3,1:2)
Index exceeds matrix dimensions.
What was the expected result of what you tried? Maybe we can guide you to the correct syntax to get the result you wanted.

댓글 수: 4

Darnell Gawdin
Darnell Gawdin 2020년 4월 28일
Hello, thank you for your time.
I'm in the reading through the manual and poking around at some of the examples to understand how it all works.
I've just tried the following and it worked, I believe I got it
Is it specifying rows 1 to 2 and columns 1 to 3? Pretty sure this is it.
copyOfC = C(1:2,1:3)
Stephen23
Stephen23 2020년 4월 28일
Darnell Gawdin's "Answer" moved here:
I did not understand fully that this is a 2 x 3 matrix and the indices represent a range of cells
I tried this to essentially copy all the cells to a new variable
CopyArray = C(1:2,1:2,1:2)
This did not work because I've added an extra dimension that isn't there. Its not a x,y,z array only a x,y array.
I've changed it to ...
CopyArray = C(1:2,1:3)
the indices now specify a range of rows from 1 to 2 and column range 1 to 3.
James Tursa
James Tursa 2020년 4월 28일
편집: James Tursa 2020년 4월 28일
Yes, C(1:2,1:3) is the subset of C containing rows 1-2 and columns 1-3 of C.
Darnell Gawdin
Darnell Gawdin 2020년 4월 28일
thanks for your help, I needed a little push.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

질문:

2020년 4월 27일

댓글:

2020년 4월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by