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

16 次查看(过去 30 天)
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