Comparing Matrices and Printing the result of the comparison
조회 수: 3 (최근 30일)
이전 댓글 표시
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
Jan
2015년 10월 20일
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.
답변 (1개)
Guillaume
2015년 10월 20일
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Entering Commands에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!