필터 지우기
필터 지우기

how can i prompt user to enter elements for specific row/columns

조회 수: 4 (최근 30일)
Austin
Austin 2013년 10월 1일
댓글: Image Analyst 2021년 1월 18일
script ive used:
m = input('Enter number of rows: ');
n = input('Enter number of columns: ');
for i = 1:m
for j = 1:n
str = ['Enter element in row ' num2str(i) ', col ' num2str(j) ': '];
a(i,j) = input(str);
end
end
a
but this script prompts the user for to enter all elements while i need only to prompt the user for specific rows and columns,can anyone kindly advise me on this?

채택된 답변

Image Analyst
Image Analyst 2013년 10월 1일
편집: Image Analyst 2021년 1월 18일
Let's say that you've stored the row, column combinations that need replacing in an array called rc. Then just do
numberOfElements = size(rc, 1);
for k = 1 : numberOfElements
row = rc(k, 1);
column = rc(k, 2);
str = sprintf('Enter the value to go into row %d, column %d : ', row, column);
a(row, column) = input(str);
end
a
This will ask for only the table elements that you've specified in advance rather than all of them. For a little nicer program you should use inputdlg() instead of input().
  댓글 수: 3
Walter Roberson
Walter Roberson 2021년 1월 18일
The array rc is the array that describes which rows and columns you want to enter the values for. The first column of rc would be the row numbers, and the second column of rc would be the column numbers.
size(rc,1) represents the number of different locations that you want to prompt for. It would be less than or equal to the total number of elements in the matrix a . You could, for example, prompt to replace 5 elements out of a 75 x 43 array, and in that case size(rc,1) would be 5, the number of places you were going to replace.
m = 75;
n = 43;
a = zeros(m,n);
r = [randi(m, 5, 1), randi(n, 5, 1)]; %r now describes 5 random places to store into
Image Analyst
Image Analyst 2021년 1월 18일
tbaracu:
Here is a full working demo:
a = ones(5, 14)
% Define which rows and columns need to be replaced.
rc = [
1, 2;
5, 12
3, 8]
numberOfElements = size(rc, 1);
for k = 1 : numberOfElements
row = rc(k, 1);
column = rc(k, 2);
str = sprintf('\nEnter the value to go into row %d, column %d : ', row, column);
a(row, column) = input(str)
end

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by