Dividing a Square Matrix into Four Distinct Matrices Based on indexing element as well as the last digit
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
dani elias
il 15 Giu 2023
Commentato: chicken vector
il 16 Giu 2023
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 Commenti
Stephen23
il 16 Giu 2023
@dani elias: sure, I have already read this thread. Your comment does not address any of my points.
Risposta accettata
chicken vector
il 15 Giu 2023
Modificato: chicken vector
il 15 Giu 2023
This will give you what you are looking for, except for the inverted number in case of odd number of digits:
A = [10902425 3040701; 36904080 304];
out = cellfun(@(x) reshape(x, 2, 4)', cellstr(string(A'.*10.^(8 - strlength(string(A'))))), 'UniformOutput', false);
out = cellfun(@(x) reshape(str2num(reshape(x, 2, 4)'),2,2)', cellstr([out{:}]), 'UniformOutput', false);
EDIT:
This is what you asked for.
Be careful that we are using numbers so in the final output 04 or 00 become 4 and 0.
B = cellstr(string(A'.*10.^(8 - strlength(string(A'))) - rem(strlength(string(A')),2).*((A'./10 - floor(A'./10)).*10).*10.^(7 - strlength(string(A'))).*9));
out = cellfun(@(x) reshape(x, 2, 4)', B, 'UniformOutput', false);
out = cellfun(@(x) reshape(str2num(reshape(x, 2, 4)'),2,2)', cellstr([out{:}]), 'UniformOutput', false);
Output:
out{1}
out{2}
out{3}
out{4}
5 Commenti
chicken vector
il 16 Giu 2023
Modificato: chicken vector
il 16 Giu 2023
As I said at the beginning:
"This will give you what you are looking for, except for the inverted number in case of odd number of digits:"
You can disregard the first method.
Actually the second method is the same with some additional manipulation of odd digits numbers.
A = [10902425 3040701; 36904080 304];
This add the trailing zeros to the number:
A'.*10.^(8 - strlength(string(A')))
This check what numbers have odd digits:
rem(strlength(string(A')),2)
This takes the last digit of every number:
((A'./10 - floor(A'./10)).*10)
So together retain only the last digit of the odd digit numbers:
rem(strlength(string(A')),2).*((A'./10 - floor(A'./10)).*10)
This inverts the digit by substracting by 9:
10.^(7 - strlength(string(A'))).*9
Example: to make 4000 become 400 you substract 10^3*(9*4))
where 4 is the last digit given by:
rem(strlength(string(A')),2).*((A'./10 - floor(A'./10)).*10);
And 10^3*9 is given by:
10.^(7 - strlength(string(A'))).*9;
The rest of the code is a series of 3 reshaping manipulations, 2 acting on char arrays and the last one on a matrix.
Più risposte (1)
Stephen23
il 16 Giu 2023
Modificato: Stephen23
il 16 Giu 2023
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 Commenti
Stephen23
il 16 Giu 2023
"Your assistance in solving the question was invaluable. I truly appreciate your expertise and guidance."
Vedere anche
Categorie
Scopri di più su Data Type Conversion in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!