Construct a 5 x 10 array that contains zeros. Assign elements at (row, column) (3,5), (2,4) and (1, 9) values of 44. Display the matrix.

I am unable to assign the values to the specific element

댓글 수: 4

This sounds like a homework assignment. If it is, show us the code you've written to try to solve the problem and ask a specific question about where you're having difficulty and we may be able to provide some guidance.
If you aren't sure where to start because you're not familiar with how to write MATLAB code, I suggest you start with the MATLAB Onramp tutorial (https://www.mathworks.com/support/learn-with-matlab-tutorials.html) to quickly learn the essentials of MATLAB.
I did a=zeros(5,10) and then I was trying a(3,5) = 44. I don’t know how to do all three simultaneously.
Nothing in that problem statement you posted states that you need to assign to those three locations simultaneously.
There are ways to do it (linear indexing is one) but unless something else in the assignment tells you not to I'd assign to those three elements sequentially.
Could you give me an example of both simultaneously and sequentially

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

답변 (2개)

DGM
DGM 2021년 9월 16일
편집: DGM 2021년 9월 16일
Consider an altered example:
In a 3x5 array, assign a fixed value to elements (3,3), (2,5), and (1,1)
sz = [3 5]; % array size
A = zeros(sz);
A(sub2ind(sz,[3 2 1],[3 5 1])) = 37
A = 3×5
37 0 0 0 0 0 0 0 0 37 0 0 37 0 0
This uses linear indexing to do the job. In order to calculate the indices, sub2ind() is used to convert the row and column subscripts specified in the two vectors. Linear indices are calculated column-major, so the indices for this example would be [9 14 1].
M = zeros(5, 10)
M = 5x10
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
M(1, :) = 15:-3:0 % Row 1
Unable to perform assignment because the size of the left side is 1-by-10 and the size of the right side is 1-by-6.
M(2, :) = 10:7:73 % Row 2
M(3, 7:9) = [3, 2, 1] % Row 3
M(4, :) = 10 % Row 4
M(5, :) = linspace(12, 36, 10) % Row 5

카테고리

도움말 센터File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

질문:

2021년 9월 16일

답변:

2024년 2월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by