Write a MATLAB code that modifies the values in MockMatrix using the following conditions:
  1. A nested for loop must be used
  2. Replace values between 100 and 200 (inclusive) with 100
  3. Replace values between 600 and 700 (inclusive) with 600
Determine the sum of all the values in this new modified matrix. Use fprintf('%16.0f', total) to print all of the digits.
i've attached the data here. thank you.

댓글 수: 4

Geoff Hayes
Geoff Hayes 2021년 2월 1일
Batrisyia - this seems like homework, so please show us the code that you have written to attempt solving the question. If there is something in particular that you are having trouble with, then please post that as a question within this thread.
Batrisyia Mohamad Asri
Batrisyia Mohamad Asri 2021년 2월 1일
y = importdata('mock_Q3file.txt');
num_rows=size(y,1);
num_cols=size(y,2);
for i=1:num_rows
for j=1:num_cols
if (y(i,j)>=100 && y(i,j)<=200)
y(i,j)=100;
elseif (y(i,j)>=600 && y(i,j)<=700)
y(i,j)=600;
end
end
end
this is what i've done so far. How do i get the sum of all the values in the modified matrix?
Tayyab Khalil
Tayyab Khalil 2021년 2월 1일
Batrisyia Mohamad Asri
Batrisyia Mohamad Asri 2021년 2월 2일
Okay thank you!

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

답변 (1개)

Abhishek
Abhishek 2025년 4월 28일

0 개 추천

To perform the required operations and get the sum of all the values in the modified matrix, you can make use of the sum()” method.
Here is the step-by-step process:
  1. Import the file into a matrix.
y = importdata('mock_Q3file.txt');
2. Use the loops to iterate through each element and apply the replacements as required.
3. After modifying the matrix, calculate the sum of all elements using the following:
total = sum(y(:));
4. Print the results.
Please refer to the documentations for more details:
Hope this helps.

카테고리

도움말 센터File Exchange에서 MATLAB에 대해 자세히 알아보기

질문:

2021년 2월 1일

답변:

2025년 4월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by