Info

この質問は閉じられています。 編集または回答するには再度開いてください。

syntax for a sucessful switch case

5 ビュー (過去 30 日間)
Axel Yantosca
Axel Yantosca 2020 年 10 月 23 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
% So I am trying to write either a loop or a switch case to iterate turns until one of the following cases are met.
% Where am I going wrong with the syntax.
% Part A Creat a Board
Bingo_board = randi([1 100], 5,5);
NewGame = zeros (5,5);
NewGame = Bingo_board
Scorecard = zeros (5,5);
Scorecard(3,3)=1
% Part B Turn 1
% Draw a Random Number
x = randi ([1,100], 1)
% Find the number on scorecard
idx = x==NewGame;
[R,C] = find(idx);
% Replace the Zero with a one on the scorecard
Scorecard (R,C) = 1
switch
case 'first'
Scorecard (1,:) = 1
case 'second'
Scorecard (2,:) = 1
case 'third'
Scorecard (3,:) = 1
case 'fourth'
Scorecard (4,:) = 1
case 'fifth'
Scorecard (5,:) = 1
case 'sixth'
Scorecard (:,1) = 1
case 'seventh'
Scorecard (:,2) = 1
case 'eighth'
Scorecard (:,3) = 1
case 'ninth'
Scorecard (:,4) = 1
case 'tenth'
Scorecard (:,5) = 1
end
  1 件のコメント
Rik
Rik 2020 年 10 月 24 日
Did you read the documentation for switch? It contains examples you could modify for your code.
And what is your intended end result? It might be possible to simplify this code.

回答 (1 件)

Walter Roberson
Walter Roberson 2020 年 10 月 24 日
you need to switch an expression. As your cases are all character vectors you would need to follow switch by an expression that evaluates to a character vector.
This is going to be a problem with your existing code as you do not have any character vectors in your code otherwise.
  1 件のコメント
Walter Roberson
Walter Roberson 2020 年 10 月 24 日
switch true
case all(Scorecard(:, 1)==1)
'first'

この質問は閉じられています。

Community Treasure Hunt

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

Start Hunting!

Translated by