Matrix help please?

This is the problem:
You should see two 4x6 matrices, A and B in your workspace. Create a script file and include the command: load HW8 in your script. Use nested for loops to accomplish the following:
  • Create a matrix C that has entries with the maximum of A and B (i.e., if A(r,c) exceeds B(r,c) then C(r,c) equals A(r,c) otherwise C(r,c) = B(r,c)). Display matrix C after the for loops.
  • Count how many entries in Matrix A are greater than the corresponding entries in B. Display the count after the for loops.
  • Count how many entries in Matrix B are greater than the corresponding entries in A. Display the count after the for loops.
  • Count how many entries in Matrix A are equal to the corresponding entries in B. Display the count after the for loops.
But I'm already stuck at the very beginning. This is all I have so far. Help appreciated
load HW8
[rows, cols] = size(A);
C = zeros(1,rows);
for row = 1:rows
if A(row,col) > B(row,col)
C(row,col) = A(row,col);
else C(row,col) = B(row,col);
end
end
display(C)

Respuestas (1)

Thorsten
Thorsten el 12 de Nov. de 2015

0 votos

You need another loop
for col = 1:cols
nested within the "for row" loop.

3 comentarios

Aaron Zorzi
Aaron Zorzi el 12 de Nov. de 2015
That worked, thank you! Could you help me in how to do the other parts? Once I get one of them I know I can do all three.
Thorsten
Thorsten el 13 de Nov. de 2015
What have you done so far to solve the first part?
Jan
Jan el 13 de Nov. de 2015
@Aaron: Use counters: Create variables before the loop, increment them insider the loop:
a = a + 1;
And use disp to show the results.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 12 de Nov. de 2015

Comentada:

Jan
el 13 de Nov. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by