Hi :) I'm learning Matlab by myself with some tutorial that i found online. I know, I'm a newbie but i can't solve this exercise. Could someone help me? Should be very easy cause i found this in a starter course.
Create a 6(rows)x4(columns) matrix filled with random integer values between 1 and 20; Create a zeroes matrix of the same dimensions; Replace each zero with a random number from the other matrix; Display each value in the new mixed matrix
Thank you :)

 채택된 답변

Andreas Goser
Andreas Goser 2016년 2월 26일

0 개 추천

To get you started, check
doc rand
doc zeros
Probably this is the easy part. If you get to "Replace each zero with a random number from the other matrix;", you may want to share all the code you have to that time in a new question here with your specific struggle (if any).

댓글 수: 3

Nicholas Menghi
Nicholas Menghi 2016년 2월 26일
편집: Stephen23 2016년 2월 26일
Thank you for your answer, this is what I did:
mat1 = (rand(6, 4))
mat2= zeros(6,4)
for i= [1,1]:length(mat1)
for k = [1,1]:size(mat1)
mat2(i) = mat1(i)
mat2(k) = mat1(k)
end
end
disp(mat1)
disp(mat2)
But Random number are not integer and i can replace only the first column.
Stephen23
Stephen23 2016년 2월 26일
편집: Stephen23 2016년 2월 26일
"But Random number are not integer": Here are three very easy ways you can locate the correct way to generate random integers:
  • use any internet search engine.
  • open the rand page, and browse the Contents on the left-hand-side.
  • open the rand page and click on the links at the very bottom of the page (to related functions).
If you had done any of these things you would immediately find out that there is a function for generating random integers, and rand is not it.
The indexing problem you would also solve in 0.5 seconds using any internet search engine. But here it is, just in case you don't have that much time:
mat2(row,col) % how to index rows and columns
Nicholas Menghi
Nicholas Menghi 2016년 2월 26일
편집: Nicholas Menghi 2016년 2월 26일
Ok, now I'm stuck here
mat1 = randi(20,6,4)
mat2 = zeros(6,4)
for i= [(1:6),(1:1)]
for k = [(1:1)]:[(1:4)]
mat2(i) = mat1(i)
mat2(k) = mat1(k)
end
end
disp(mat1)
disp(mat2)
I don't know, it changes again only the first column... 0.5... secs now I am feeling a little wretched.

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

추가 답변 (0개)

카테고리

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

태그

질문:

2016년 2월 26일

편집:

2016년 2월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by