Comparing Matrices and Printing the result of the comparison

2 visualizaciones (últimos 30 días)
Chameleon17
Chameleon17 el 20 de Oct. de 2015
Respondida: Guillaume el 20 de Oct. de 2015
Good Afternoon,
I am looking for a bit of advice.
If I had say:
Mat1 = [0 0 0 0 0 0; 0 0 13 0 0 0; 0 0 0 0 25 26; 0 0 0 0 0 0; ...
41 42 43 44 45 46; 0 0 0 54 55 56; 61 62 63 0 0 0]
Mat2 = [0 0 0 0 0 0; 0 0 0 0 0 0; 0 0 0 0 25 0; 0 0 0 34 0 0; ...
0 0 0 0 0 46; 50 0 0 0 0 0; 0 0 63 0 0 0]
I know what I want to do.
Working by rows -
(1) If for row 1:7 of Mat2 all values are 0 in a row, then find if all values of this row in Mat1 are also zero. I want to use fprintf I think to print a response dependent on which criteria are met, and all the values that are present if they are not zeros in the corresponding row of Mat1.
(2) If for row 1:7 of Mat2, if there is a value, I would like to compare that row to the corresponding row of Mat1 and print the values for the row in Mat1 first which come before and including the position of Mat2 and then also those values that come after.
I'm just looking for any advice, I think i'm going about it all in an obtuse way...
Thank you for any time you can spare or advice you can offer, and I'm sorry if I've not explained it very well.
  1 comentario
Jan
Jan el 20 de Oct. de 2015
Please note that the number of leading spaces mean different formats in this forum. I've tried to clean up your message a little bit.

Iniciar sesión para comentar.

Respuestas (1)

Guillaume
Guillaume el 20 de Oct. de 2015
For a given row:
%row: row index
if any(Mat2(row, :))
%Mat2 has at least a non-zero value in the row
%assumption, there's only one non-zero value in mat2
pos = find(Mat2(row, :));
mat1before = nonzeros(Mat1(row, 1:pos-1));
mat1after = nonzeros(Mat1(row, pos+1:end));
fprintf('row %d is non-zero with value %d, before: %s, after: %s\n', ...
row, Mat2(row, :), num2str(mat1before), num2str(mat2before));
else
%Mat2 i all zero in the row
fprintf('row %d is all zero, mat1 values: %s\n', ...
row, nonzeros(Mat1(row, :)));
end
wrap in loop going over the rows

Categorías

Más información sobre Entering Commands en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by