how can i prompt user to enter elements for specific row/columns
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Austin
am 1 Okt. 2013
Kommentiert: Image Analyst
am 18 Jan. 2021
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?
0 Kommentare
Akzeptierte Antwort
Image Analyst
am 1 Okt. 2013
Bearbeitet: Image Analyst
am 18 Jan. 2021
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 Kommentare
Walter Roberson
am 18 Jan. 2021
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
am 18 Jan. 2021
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
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!