Array contents display in pairs - arrays

I have an array for example: A=[01 255 03 122 85 107]; and I want to print the contents as
A=
FF 01
7A 03
6B 55
Basically a read out from a memory. Is there any function in MatLab lib? I need to do this with minimum use of loops.

Use this -
str2num(num2str(fliplr(reshape(A,2,[])'),'%1d'))
Output -
ans =
21
43
65
87
If you only want to print it as characters, use it without str2num, like this -
num2str(fliplr(reshape(A,2,[])'),'%1d')
Output -
ans =
21
43
65
87
General case with zeros padding -
A=[1 2 3 4 5 6 7 8 9 3] %// Input array
N = 3; %// groupings, i.e. 2 for pairs and so on
A = [A zeros(1,N-mod(numel(A),N))]; %// pad with zeros
out = str2num(num2str(fliplr(reshape(A,N,[])'),'%1d'))
Output -
out =
321
654
987
3
Edit for hex numbers :
Ar = A(flipud(reshape(1:numel(A),2,[])))
out1 = reshape(cellstr(dec2hex(Ar))',2,[])'
out2 = [char(out1(:,1)) repmat(' ',[numel(A)/2 1]) char(out1(:,2))]
Output -
out1 =
'FF' '01'
'7A' '03'
'6B' '55'
out2 =
FF 01
7A 03
6B 55

Related

How do I separate an array depending on values in a column?

Say I have an Mx4 array A where the values in the first column are a number 1 to 12. Now I want to gather the rest of the columns in 12 separate Mx3 arrays depending on which number is in column 1.
How would I go about doing that?
You can use unique and splitapply as follows. The result is a cell array of arrays.
M = [2 11 41 51;
1 10 20 30;
1 62 83 22;
4 73 53 53;
2 84 94 14]; % example data
L = 5; % Group labels are 1:L (L=12 in your case)
[u,~,w] = unique(M(:,1));
result = cell(L,1);
result(u) = splitapply(#(x){x}, M(:,2:end), w);
This gives
>> celldisp(result)
result{1} =
10 20 30
62 83 22
result{2} =
11 41 51
84 94 14
result{3} =
[]
result{4} =
73 53 53
result{5} =
[]

How to create a 4x4 array with hexadecimal input?

How can I create a 4x4 array with hexadecimal input in matlab?
I'm currently getting this error:
Error using reshape
To RESHAPE the number of elements must not change.
key =
16×2 char array
'41'
'42'
'43'
'44'
'45'
'46'
'47'
'48'
'49'
'4A'
'4B'
'4C'
'4D'
'4E'
'4F'
'50'
w = (reshape (key, [4, 4]))';
Hexadecimal numbers in MatLab are actually text string.
In your case, you do not have a (16 x 1) array, but a 16 x 2 string array.
You can have a 4 x 4 array of decimal data by converting the hexadecimal values using the hex2dec function:
new_key=reshape(hex2dec(key),4,4)
Which gives:
new_key =
65 69 73 77
66 70 74 78
67 71 75 79
68 72 76 80
You can have a 4 x 4 string array of hexadecinal data by firdt converting the input array into a cell array:
c_key=cellstr(key)
c_key_1=reshape(c_key,4,4)
Which gives:
c_key_1 =
{
[1,1] = 41
[2,1] = 42
[3,1] = 43
[4,1] = 44
[1,2] = 45
[2,2] = 46
[3,2] = 47
[4,2] = 48
[1,3] = 49
[2,3] = 4A
[3,3] = 4B
[4,3] = 4C
[1,4] = 4D
[2,4] = 4E
[3,4] = 4F
[4,4] = 50
}
NOTE: Tested only with Octave

Match vectors of different length by their maxima and get indices

I have variables
A = [40 67 68 70 66 65 99 90 65 20 21]
B = [1 1 2 3 1]
How to get indices if A by matching the maxima of B and A?
So imagine I slide with B over A, stop when the maxima match, and then I'd like to get the "position" of B by means of the according indices of A.
desired result :
4 5 6 7 8
One way of many:
A = [40 67 68 70 66 65 99 90 65 20 21]
B = [1 1 2 3 1]
%// maxima
[~,mA] = max(A(:))
[~,mB] = max(B(:))
%// result
mDiff = mA - mB
idx = ( mDiff + 1 ) : ( mDiff + numel(B) )

How to use nested or double for loop in Matlab to generate new 1D array by comparing Existing 1D array

For example
I have one binary array with size of 9 as b = [0 1 0 1 0 1 1 1 1], Then another array 'm' with size of 7 as m = [21 28 36 45 45 66 66]. Here i want to change all the zeros of 'b' by 1st element of m then replace 1's of b by consecutive elements of 'm' so my output 1D array should be like k = [21 28 21 36 21 45 45 66 66].
Below is my code i really don't know where i did mistake please help me to solve this
b= [0 1 0 1 0 1 1 1 1];
b=b(:);
m = [21 28 36 45 45 66 66];
m = m(:);
k=zeros(size(b));
for i=1:length(b)
for j=2:length(m)
if b(i)==0
k(i)=m(1);
else
k(i)=m(j);
end
end
end
am getting output as
k = [21 66 21 66 21 66 66 66 66]
Use logical indexing instead - it is much faster and more readable:
b = [0 1 0 1 0 1 1 1 1];
m = [21 28 36 45 45 66 66];
k = zeros(size(b));
k(b==0) = m(1); % fill values where b=0 with m(1)
k(b==1) = m(2:sum(b)+1); % fill values where b=1 with consecutive m values
Result:
>> k
k =
21 28 21 36 21 45 45 66 66

Multiple array items appending

This is the reproducible code:
a <- rep(1, 20)
a[c(1, 12, 15)] <- 0
b <- which(a == 0)
set.seed(123)
d <- round(runif(17) * 100)
I would like to append 0s to d to get the following result:
[1] 0 29 79 41 88 94 5 53 89 55 46 0 96 45 0 68 57 10 90 25
that is equal to d after appending 0s to each element which has index equal to b - 1.
I've seen append() accepts just one single "after" value, not more than one.
How could I do?
Please, keep in mind I cannot change the length of d because it is supposed it's the ouput of a quite long function, not a simple random sequence like in this example.
You can use negative subscripting to assign the non-zero elements to a new vector.
D <- numeric(length(c(b,d)))
D[-b] <- d
D
# [1] 0 29 79 41 88 94 5 53 89 55 46 0 96 45 0 68 57 10 90 25

Resources