while strcmp(Str,'yes')
n = input('number or rows');
m = input('number of columns');
A = zeros(n,m);
for i = 1:n
for j = 1:m
A(i,j)= i;j;
end
end
end
i am trying to make an array where i first ask the user to enter n and m and then use a for loop to make the array, can someone explain and help me figure out what i did and what i need to do to make it work? thanks

11 件のコメント

Walter Roberson
Walter Roberson 2020 年 2 月 22 日
Your code to create the array looks okay.
However
A(i, j) = i;j;
is the same as
A(i, j) = i;
j;
The first line would assign to each element its own row number. The second line would call j into memory, assign it to the variable named 'ans', and then discard the value without displaying anything.
The way your code is written hints that you possible want to store both i and j at A(i, j). However, A(i, j) only has room for a single number.
Ana Jurado
Ana Jurado 2020 年 2 月 22 日
oh okay that makes sense, thank you. how do i get the numbers that the users picks to be put into the array? because nothing that I'm doing seems to be working
Walter Roberson
Walter Roberson 2020 年 2 月 22 日
For any given array element indexed at (i,j), what should be the rule about what should be stored there?
Ana Jurado
Ana Jurado 2020 年 2 月 22 日
my instructions just " Use a for loop to input the n x m numbers into the array "
Image Analyst
Image Analyst 2020 年 2 月 22 日
For example, is A(i,j) equal to i+j, or i*j, or 1234*i + 213123*j^2? We have no idea what you want.
Walter Roberson
Walter Roberson 2020 年 2 月 22 日
Maybe the integers from 1 to m*n? If so it can be done in a single easy loop using numel() and linear indexing
Ana Jurado
Ana Jurado 2020 年 2 月 22 日
i will ask what is supposed to be there, but thank you so much, i actually understood this a lot more than before. thank you!!
Walter Roberson
Walter Roberson 2020 年 2 月 22 日
Read about "linear indexing". You might not end up using it this time, but it will come in handy many times.
Ana Jurado
Ana Jurado 2020 年 2 月 22 日
i'll go check it out, thanks!
Image Analyst
Image Analyst 2020 年 2 月 22 日
Put the user-entered numbers into the array in what order? Row by row, or column by column? Like what gets filled first: the first row, or the first column?
Ana Jurado
Ana Jurado 2020 年 2 月 22 日
it doesn't say :/

サインインしてコメントする。

回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Matrix Indexing についてさらに検索

タグ

質問済み:

2020 年 2 月 22 日

コメント済み:

2020 年 2 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by