How to compare 2 rows with specific row gap

I have a matrix called myMatrix. In the below code I generated a 256 bit response by comparing ROW1 with ROW2, ROW3 with ROW4 etc.
How can I modify the code to generate a 256 bit response by comparing ROW1 with ROW17, ROW2 with ROW18 etc.
output_bit=zeros(256,193);
for s=1:193;
p=1;
for q=1:2:512;
if myMatrix(q,s) > myMatrix(q+1,s);
output_bit(p,s)=1;
else
output_bit(p,s)=0;
end
p=p+1;
end
end

 採用された回答

Guillaume
Guillaume 2020 年 2 月 28 日
編集済み: Guillaume 2020 年 2 月 28 日

1 投票

In matlab loops are rarely needed and often make the code more complicated. Case in point, your original code comparing pairs of consecutive rows could be replaced by just:
output_bits = MyMatrix(1:2:end, :) > MyMatrix(2:2:end, :);
With your new question, what should happen after you've compared row 16 with row 32? Compare row 17 with row 33 or skip to comparing row 33 with row 49?

8 件のコメント

Farhan K
Farhan K 2020 年 2 月 28 日
It should skip because the row compare should be in pairs.
Farhan K
Farhan K 2020 年 2 月 28 日
I tried by replacing the new code. it works but it shows error in the later part of my full code. So I have to stick to the loop thing :(
Guillaume
Guillaume 2020 年 2 月 28 日
My code above will work as long as MyMatrix has an even number of rows. If it's not the case, you need to explain what should happen with the extra pair-less row.
Which brings the next question for the skip-16. What should be done if the matrix height is not a multiple of 32?
Farhan K
Farhan K 2020 年 2 月 28 日
編集済み: Farhan K 2020 年 2 月 28 日
It has even number of rows. Can you please help on how to compare ROW1 with ROW17, ROW2 with ROW18 etc as pairs using your code then?
Image Analyst
Image Analyst 2020 年 2 月 29 日
Did you try
output_bits = MyMatrix(1:2:end-17, :) > MyMatrix(17:2:end, :);
Farhan K
Farhan K 2020 年 3 月 1 日
I tried this but the answer is not matching, something might be wrong
Guillaume
Guillaume 2020 年 3 月 1 日
With no answer to my question "What should be done if the matrix height is not a multiple of 32", I'm assuming that the matrix height is a multiple of 32. In that case:
assert(mod(size(MyMatrix, 1), 32) == 0, 'Matrix height is not a multiple of 32');
temp_matrix = reshape(MyMatrix, 16, [], size(MyMatrix, 2)); %reshape into 16 rows, and move columns into the 3rd dimension
output_bits = reshape(temp_matrix(:, 1:2:end, :) > temp_matrix(:, 2:2:end, :), [], size(MyMatrix, 2)); %compare pair of columns along 3rd dimension (originally 2nd) and reshape back into matrix
Farhan K
Farhan K 2020 年 3 月 1 日
It worked man!!! Wow thanks a lot bro :D

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

質問済み:

2020 年 2 月 28 日

コメント済み:

2020 年 3 月 1 日

Community Treasure Hunt

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

Start Hunting!

Translated by