A = [7,2,3,4,4]
I want to combine them to:
B = 72344
I am new to matlab. Is there any build in function that can do that?
Thanks in advance.
Here is a solution
>> A = [7,2,3,4,4];
>> B=A*(10.^(length(A)-1:-1:0))'
B = 72344
Note #BenVoigt's comment:
>> A = [7,2,3,4,4;2,3,4,5,3]
A =
7 2 3 4 4
2 3 4 5 3
>> B=A*(10.^(length(A)-1:-1:0))'
B =
72344
23453
zz = str2num(num2str(A(:))')
zz =
72344
is straightforward
You could also simply add '0' as a shift into the range of numeric characters: A+'0'. Then, B = str2double(char(A+'0')).
You can apply num2str and strrep as follows:
>> A = [7,2,3,4,4];
>> B = str2num(strrep(num2str(A(:)'),' ',''))
B =
72344
Note that A(:)' is used to ensure a row vector. However, webpat's answer is more concise since you can leave off the ' and strrep is not required. Also, the mathematical solution by damienfrancois seem more elegant than using strings.
Related
I have an array a of size ZxW.
Z = 20;
W = 30;
A = 40; %will be used below
size(a)
20 30
Then I apply to a two different transformations, and then after those I delete a and I cannot go back to it.
First transformation:
b = repelem(a(:,1),A,A);
Second transformation:
c = repmat(a,[1,1,A,A]);
d = c(:,1,:,:);
After those transformations and deleting a (which cannot be used for the following), I want to compare d and b using
assert( isequal(b,f) )
Where f is a transformation of d that makes the assertion true.
My first idea was a simple reshape:
f = reshape(squeeze(d),[Z*A,A]);
Which does not work as repelem and repmat move entries differently. How can I do this?
Thanks for the attention.
Sincerely
Luca
EDIT: changed
c = repmat(a,[A,A]);
with
c = repmat(a,[1,1,A,A]);
The answer (by Jan Simon) is:
f = reshape(permute(d, [3,1,4,2]), [Z*A,A]);
isequal(b, f) % 1: equal
Thanks for the help.
Luca
NOTE: This solution is obsoleted with the edited question, please refer to the other solutions posted.
Look closely at what the pattern of b and d are:
b = [1 1 1 2 2 2 3 3 3].'
d = [1 2 3 1 2 3 1 2 3].'
Hence, the transformation f can be:
f = reshape(d, [A, A]).'
f = f(:)
This will convert d to be exactly b or vice versa.
I want to do multivariable assignment. I can do [a,b] = min([1 2 3]) but I can't do [a,b] = [1,2]. Why? Is there any workaround?
The [1,2] on the right hand side of the assignment is interpreted as array with the two elements 1 and 2.
If you want to do the multi-variable-assignment in one line, you can use deal in Matlab. This should work in Octave as well according to the documentation here.
>> [a,b] = deal(1,2)
a =
1
b =
2
The advantage of using deal is that it works in Matlab as well, where the solution with [a b] = {1 2}{:} won't.
Octave basics: How to assign variables from a vector
>> [a b c] = {5 6 7}{:}
a = 5
b = 6
c = 7
To adapt Cobusve's answer to Matlab, two lines are required:
>> h={5 6 7}
h =
[5] [6] [7]
>> [a b c]=h{:}
a =
5
b =
6
c =
7
The questions is:
A=[9 10];
And I want to obtain B={'09','10'};
I made this:
for hij=1:size(A,1)
if A{hij}<9
B{hij}=strcat('0',num2str(A{hij}),'');
else
B{hij}=strcat('',num2str(A{hij}),'');
end
end
But I was wondering if there is any posibility to make that without using a loop, maybe using a "cellfun"; thanks!
Is this what you want?
>> B = num2str(A(:),'%02d'); %// second argument to num2str is format spec
B =
09
10
This gives a string matrix B. To convert B into a cell array of strings:
>> B = mat2cell(B,ones(1,size(B,1))).';
B =
'09' '10'
or, as noted by Divakar,
>> B = cellstr(B).';
B =
'09' '10'
Luis answer is good. Just for completeness, you can use arrayfun if you really want:
C = arrayfun(#num2str, A,'UniformOutput', 0 );
Problem statement:
Take as input an array of digits (e.g. x = [1 2 3]) and output an array of digits that is that number "incremented" properly, (in this case, y = [1 2 4]).
Examples of proper input/output:
x = [1 9 1 9] ----> y = [1 9 2 0]
and
x = [9 9 9] ----> y = [1 0 0 0]
I thought the easiest solution would be to convert the vector to a matrix, increment it, and then convert back. It sounds convoluted but it worked better than trying to do the addition normally.
My attempt:
function ans = incrementor(x)
x=sprintf('%1d',x)
x=str2num(x)
num2str(x+1) - '0'
Can anyone come up with a more efficient solution?
(I'm thinking there may be a command to perform the addition directly without converting to a scalar, but I don't know it.)
I believe that matlab has no native function for what you need, at some point you will have to convert your values in scalar mode to increase
Combine the digits, add 1,
yn=x*(10.^(length(x)-1:-1:0))' + 1
Then convert back:
numDigits = floor(log10(yn)+1)
tens = 10.^(1:numDigits);
y = fliplr(floor(mod(yn,tens)./(tens/10)))
Or, as the OP (user3020151) points out, you can convert back with num2str:
y = num2str(yn) - '0'
Addressing the search for a single command, I do not know of a built-in command, but you can construct an anonymous function as follows:
>> incrementor = #(x) num2str(x*(10.^(length(x)-1:-1:0)).' + 1) - '0'; %' anonymous
>> x = [9 9 9];
>> y = incrementor(x)
y =
1 0 0 0
No need to use strings or convert to a number: Just find the rightmost non-nine digit (if any), add 1 to it, and set any digit to its right to zero. The case when all digits are nine needs to be dealt with separately, because then the numer of digits has to be increased.
k = find(x~=9,1,'last');
if isempty(k)
y = [1 zeros(1,numel(x))];
else
y = [x(1:k-1) x(k)+1 zeros(1,numel(x)-k)];
end
x = [1 2 3];
r1=sprintf('%1d',x);
r1= str2num(r1) + 1;
r2=sscanf( sprintf( num2str(r1) ), '%1d' )'
This question already has an answer here:
effective way of transformation from 2D to 1D vector
(1 answer)
Closed 9 years ago.
I would like to use the (:) operator and the transpose at the same time. Is this possible? Basically I would like to do something like
output = A'(:)
except that this does not work. Does anyone know a workaround?
Thanks!
Immo
The : operator in this case is shorthand for reshaping the matrix into a vector. You can work around the limitation of where you use the operator by using the reshape function explicitly:
octave> A = [1 2;3 4]
A =
1 2
3 4
octave> B=A'
B =
1 3
2 4
octave> C=B(:)
C =
1
2
3
4
octave> D=reshape(A',[],1) #% vectorize transpose in one line
D =
1
2
3
4
Try with:
output = reshape( A.', numel(A), 1);
>> A = rand(4,3);
>> output = reshape( A.', numel(A), 1);
A =
0.447213 0.046896 0.679087
0.903294 0.768745 0.651481
0.701071 0.122534 0.611390
0.535844 0.478595 0.772810
output =
0.447213
0.046896
0.679087
0.903294
0.768745
0.651481
0.701071
0.122534
0.611390
0.535844
0.478595
0.772810
Beware that reshape reads the matrices accessing along columns so you may not need to transpose the matrix A.
Also, remember that the operator ' is the hermitian operator, namely, conjugated of the transposed, whereas .' is simply transposition, which you could also get by transpose(A).
You may want to do everything in a single line without re-typing all every time. One solution is creating a function handles as boop:
>> boop = #(x) reshape( transpose(x), numel(x), 1)
>> output = boop(A)
output =
0.447213
0.046896
0.679087
0.903294
0.768745
0.651481
0.701071
0.122534
0.611390
0.535844
0.478595
0.772810