I have a cell with char values next to each other as seen below
Cell 1 | Cell 2
____________
'Hello' | 'World'
I would like to define a function that extracts the previous cell value along with the current cell value. I wrote the following code to do so:
t = x{1,i} - Current Cell Value
f = x{2,i-1} - Previous Cell Value, starting with Cell 2 with the previous value of Cell 1
But I keep getting the error
Index in position 2 is invalid. Array indices must be positive integers or logical values.
I believe the error is with regards to the defined function f. It has to do with the i-1 value. At this point, I am stuck. Any thoughts on how to fix that so it works?

답변 (1개)

TADA
TADA 2019년 4월 21일
편집: TADA 2019년 4월 21일

0 개 추천

The problem is you're indexing at index 0 which is invalid.
% some specific handling of
% the first cell should be
% executed here if necessary
for i = 2:size(x,2)
t = x{1,i};
f = x{2,i-1};
end

댓글 수: 6

Jeffrey Pang
Jeffrey Pang 2019년 4월 21일
Its now telling me
Index exceeds array bounds.
TADA
TADA 2019년 4월 21일
I'll have to see your code and an example of the data to know what's going on
Jeffrey Pang
Jeffrey Pang 2019년 4월 21일
I am trying to make a card game where you draw cards until you get 2 in a row (2 kings are drawn one after the other) . The feature I am trying to implement is to stop the code when two cards of the same type are drawn.
Code:
deck = {char('2 Spades'), char('2 Hearts'), char('2 Clubs'), char('2 Diamonds'), char('3 Spades'), char('3 Hearts'), char('3 Clubs'), char('3 Diamonds'), char('4 Spades'), char('4 Hearts'), char('4 Clubs'), char('4 Diamonds') , char('5 Spades'), char('5 Hearts'), char('5 Clubs'), char('5 Diamonds'), char('6 Spades'), char('6 Hearts'), char('6 Clubs'), char('6 Diamonds'), char('7 Spades'), char('7 Hearts'), char('7 Clubs'), char('7 Diamonds'), char('8 Spades'), char('8 Hearts'), char('8 Clubs'), char('8 Diamonds'), char('9 Spades'), char('9 Hearts'), char('9 Clubs'), char('9 Diamonds'), char('10 Spades'), char('10 Hearts'), char('10 Clubs'), char('10 Diamonds'), char('K Spades'), char('K Hearts'), char('K Clubs'), char('K Diamonds'), char('J Spades'), char('J Hearts'), char('J Clubs'), char('J Diamonds'), char('Q Spades'), char('Q Hearts'), char('Q Clubs'), char('Q Diamonds'), char('A Spades'), char('A Hearts'), char('A Clubs'), char('A Diamonds')};
x = randsample(deck, 52, false);
for i = 1:52;
pause(1)
disp(x{1,i});
for j = 2:size(x,2)
t = x{1,j};
f = x{2,j-1};
end
if t(1) == f(1)
pause
end
reshape([deck],1,[]);
end
TADA
TADA 2019년 4월 21일
편집: TADA 2019년 4월 21일
This is your problem
f = x{2,j-1};
As x is a one dimentional row cell array
This should work I think:
for i = 1:52
disp(x{1,i});
t = x{1,i};
f = x{1,i-1};
if t(1) == f(1)
break;
end
end
Jeffrey Pang
Jeffrey Pang 2019년 4월 23일
I got it. Thank you for putting me on the right track!
TADA
TADA 2019년 4월 23일
Cheers
Good luck

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

카테고리

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

태그

질문:

2019년 4월 21일

댓글:

2019년 4월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by