Rand Function in MATLAB

조회 수: 4 (최근 30일)
Aashita Galundia
Aashita Galundia 2020년 1월 30일
댓글: Aashita Galundia 2020년 1월 31일
I am very new to MATLAB. I'm trying to create trials using the rand function. I want four different rectangles on my figure to highlight at random.
Below is my code.
The figure is plotted with four rectangles, I want to highlight the four rectangles on random. Thanks!
%Create the figure for trial
t = figure
xlim([1 5])
ylim([1 5])
lower_l=rectangle('Position', [1.5 1.5 .5 .5]);
hold on
upper_l=rectangle('Position', [1.5 4 .5 .5]);
hold on
lower_r=rectangle('Position', [4 1.5 .5 .5]);
hold on
upper_r=rectangle('Position', [4 4 .5 .5]);
hold on
cross=text(3, 3, '+');
set(cross, 'fontsize', 20);**
%create condition without target
y = rand(lower_l, lower_r, upper_l, upper_r);
set(y, 'EdgeColor', 'r')
pause(0.1)
set(y, 'EdgeColor', 'k')
pause(0.3)
Appreciate any comments!

답변 (1개)

Cris LaPierre
Cris LaPierre 2020년 1월 30일
편집: Cris LaPierre 2020년 1월 30일
The random functions in MATLAB generate random numbers, not select a random value from a supplied vector.
What you could consider doing is creating a vector of the rectangle handles, and then randomly generate an index to extract one of them. That might look somethign like this
%create condition without target
y = [lower_l, lower_r, upper_l, upper_r];
idx = randi(length(y));
set(y(idx), 'EdgeColor', 'r')
pause(0.1)
set(y(idx), 'EdgeColor', 'k')
pause(0.3)
  댓글 수: 1
Aashita Galundia
Aashita Galundia 2020년 1월 31일
Thanks, this was very helful!

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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by