how can i prompt user to enter elements for specific row/columns
5 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Austin
il 1 Ott 2013
Commentato: Image Analyst
il 18 Gen 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 Commenti
Risposta accettata
Image Analyst
il 1 Ott 2013
Modificato: Image Analyst
il 18 Gen 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 Commenti
Walter Roberson
il 18 Gen 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
il 18 Gen 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
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!