Dividing a Square Matrix into Four Distinct Matrices Based on indexing element as well as the last digit
Show older comments
Assuming I have a square matrix of N x N, example for 2x2 we can have A = [10902425 3040701; 36904080 304], where each cell contains a number with at least eight digits. I aim to generate four distinct square matrices from A, namely A1=[10 30;36 30], A2=[90 40;90 04], A3=[24 70;40 00], and A4=[25 01;80 00]. The division of pixels is based on pairs, and a new matrix is formed. If the last number of pixel is not in pair,the put 0 as the leading number to make pair and the rest of pixel position will be 00. in a simple way each number in each position is divided into pair for example 10902425 -> 10| 90| 24| 25 , but for 304 it should be 30| 04 instead of 40 this will make easy during the reversing process (in my view). I need a help on both forwarding process as well as reversing to the original matrix
close all;
clear;
clc;
% Given matrix A
A = [10902425 3040701; 36904080 304];
% Extract the digits from A
A_digits = mod(A, 10);
% Generate A1
A1 = floor(A/100);
% Generate A2
A2 = zeros(size(A));
A2(A_digits == 0 | A_digits == 1) = mod(A(A_digits == 0 | A_digits == 1), 100);
% Generate A3
A3 = zeros(size(A));
A3(A_digits == 2 | A_digits == 4) = mod(A(A_digits == 2 | A_digits == 4), 100);
% Generate A4
A4 = zeros(size(A));
A4(A_digits == 5 | A_digits == 6) = mod(A(A_digits == 5 | A_digits == 6), 100);
% Display the resulting matrices
this code produce error
3 Comments
" need a help on both forwarding process as well as reversing to the original matrix"
The task is impossible, without extra information your algorithm cannot distinguish between e.g. 10000000 and 1.
It is also impossible to detect those swapped digits.
What is the actual goal?
dani elias
on 16 Jun 2023
Edited: dani elias
on 16 Jun 2023
Stephen23
on 16 Jun 2023
@dani elias: sure, I have already read this thread. Your comment does not address any of my points.
Accepted Answer
More Answers (1)
Simpler and more efficient.
My answer takes into account my comments here. Assumes no zero, negative, fractional, inf, etc. values.
format compact
A = [10902425,3040701;36904080,304]
B = A.*10.^(8-fix(log10(A)))
C = 10.^cat(3,7,5,3,1);
% A1=[10 30;36 30], A2=[90 40;90 04], A3=[24 70;40 00], A4=[25 01;80 00]
D = mod(fix(B./C),100)
And back again:
E = sum(D.*C,3)
6 Comments
chicken vector
on 16 Jun 2023
Edited: chicken vector
on 16 Jun 2023
Very nice and clean one! Rounding is much better than using strings indeed.
Just remember to add the digit inversion as per question:
B = A.*10.^(8-fix(log10(A))) - (log10(A)<7).*(A - floor(A./10).*10).*10.^(8 - ceil(log10(A))).*9;
chicken vector
on 16 Jun 2023
If you had to include it how would you do this?
I am sure you can find a better way than me and I learn a lot from these answers.
dani elias
on 16 Jun 2023
Stephen23
on 16 Jun 2023
"Your assistance in solving the question was invaluable. I truly appreciate your expertise and guidance."
chicken vector
on 16 Jun 2023
My pleasure
Categories
Find more on Programming 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!