How do I create a matrix that overwrites another if the value is bigger?

1 view (last 30 days)
Okay here is the problem I am stuck on:
Use a for loop to count how many entries in vector x are larger than the corresponding entry in the vector y. Also, within your for loop, create a vector that will save all of the index values where x(index) > y(index). After the for loop, use an fprintf statement to display how many entries in vector x are larger than the corresponding entry in vector y. Use a disp or fprintf statement to display the vector containing all of the index values where x(index) > y(index).
So far I have this but do not know how to go about comparing/replacing values in the matrices:
x=[1,3,-3,-2,7,10,0,6,9,9,-4,-5]
y=[-3,9,-4,1,10,3,6,0,5,8,-5,7]
count=0;
for x(1,12)>y(1,12)
count=count+1
end
fprintf('Number of Times the x value is bigger than y = %0.f\n',count);

Accepted Answer

Star Strider
Star Strider on 7 Nov 2014
You’re not setting up your loop correctly. You want to loop through both x and y to compare corresponding elements of both. This should get you started:
x=[1,3,-3,-2,7,10,0,6,9,9,-4,-5];
y=[-3,9,-4,1,10,3,6,0,5,8,-5,7];
count = 0;
for k1 = 1:length(x)
d = x(k1)-y(k1);
% increment ‘count’ here if ‘d’ is positive
end
Your fprintf call looks good.

More Answers (0)

Categories

Find more on Multidimensional Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!