I have a simple problem that I can't seem to figure out a simple solution for ...
I have a vector x
x = [1, 2, 3, 4]
I'd like to turn that vector into a scalar where the scalar is comprised of the values of x.
for example:
>> y = compressvec(x)
y =
1234
It'd be nice if the solution also worked on arrays treating each row as its own vector
for example :
x = [1, 2, 3, 4; 5, 6, 7, 8]
>> y = compressvect(x)
y =
1234
5678
>> size(y)
ans =
2 1
Here are some ways to do it:
Convert to characters, then interpret as a base-10 number:
y = base2dec(char(x+'0'), 10);
Similar, via cell array of character vectors:
y = str2double(cellstr(char(x+'0')));
Direct arithmetic approach using matrix multiplication:
y = x * 10.^(size(x,2)-1:-1:0).';
Similar, using element-wise multiplication with broadcast:
y = sum(bsxfun(#times, x, 10.^(size(x,2)-1:-1:0)), 2);
Related
Trying to replicate a calculation from Matlab in Julia but am having trouble converting a single column complex array into a sparse diagonalized array for matrix multiplication.
Here is the Matlab code I am trying to replicate in Julia:
x*diag(sparse(y))
where x is of size 60,600000 and is of type: double, and y is size 600000,1 and is of type: complex double.
You can use Diagonal for that:
using LinearAlgebra
x=rand(6,60)
y=rand(Complex{Float64},60,1)
x*Diagonal(view(y,:))
I have used view(y,:) to convert y to a Vector - this is here a dimension drop operator you can also use shorter form vec(y) instead. Depending on what you want to do you might explicitly say that you want first column by view(y,:,1).
Note that Diagonal is just a sparse representation of a matrix.
julia> Diagonal(1:4)
4×4 Diagonal{Int64,UnitRange{Int64}}:
1 ⋅ ⋅ ⋅
⋅ 2 ⋅ ⋅
⋅ ⋅ 3 ⋅
⋅ ⋅ ⋅ 4
Another option that might cover more use case scenarios are BandedMatrices:
using BandedMatrices
x*BandedMatrix(0=>view(y,:))
Note that BandedMatrix uses set of pairs for bands, where band 0 is actually the diagonal.
I guess you don't mean it like this, but one can also interpret the question in the way that y is a sparse vector in the Julia sense, and you want to construct a sparse diagonal matrix out of it. In that case you can do the following:
julia> y = sprand(10, 0.2)
10-element SparseVector{Float64,Int64} with 2 stored entries:
[4 ] = 0.389682
[5 ] = 0.232429
julia> I, V = findnz(y)
([4, 5], [0.3896822408908356, 0.2324294021548845])
julia> sparse(I, I, V)
5×5 SparseMatrixCSC{Float64,Int64} with 2 stored entries:
[4, 4] = 0.389682
[5, 5] = 0.232429
Unfortunately, spdiagm does not preserve structural zeros for a sparse input:
julia> spdiagm(0 => y)
10×10 SparseMatrixCSC{Float64,Int64} with 10 stored entries:
[1 , 1] = 0.0
[2 , 2] = 0.0
[3 , 3] = 0.0
[4 , 4] = 0.389682
[5 , 5] = 0.232429
[6 , 6] = 0.0
[7 , 7] = 0.0
[8 , 8] = 0.0
[9 , 9] = 0.0
[10, 10] = 0.0
I don't know whether this is intentional, but I filed an issue about this behaviour.
How Do i solve this summation in MATLAB without using for/while loop?
Here C is a vector(1*N matrix), n=length(c) and x is scalar.
c(1)*x^1+c(2)*x^2+c()*x^3+....+c(n)*x^n.
Or can i Create a matrix with all element equal to x but with increasing power, like x, x^2,x^3....?
There are several ways:
result = polyval(fliplr([0 c]), x);
result = sum(c.*x.^(1:numel(c)));
result = sum(c.*cumprod(repmat(x, 1, numel(c))));
As an example, for
c = [3 4 -5 2 3];
x = 9;
any of the above gives
result =
186975
Check:
>> c(1)*x^1+c(2)*x^2+c(3)*x^3+c(4)*x^4+c(5)*x^5
ans =
186975
I have a problem with defining some matrices in MATLAB. I get three numbers x,y,z as an input from user, then I want to create y-1 empty matrices. For example consider x = 3, y = 4, and z = 2. The required y-1 matrices M1, M2, and M3 are:
size(M1) = [3,4] ~ [x,y]
size(M2) = [4,4] ~ [y,y]
size(M3) = [4,2] ~ [y,z]
The parameters x,y are not known before running the program. If y was 5, the matrices were:
size(M1) = [3,5] ~ [x,y]
size(M2) = [5,5] ~ [y,y]
size(M3) = [5,5] ~ [y,y]
size(M4) = [5,2] ~ [y,z]
Indeed the main problem is that the number of matrices is an input.
Please guide me on how I can create a function loop to define this matrices.
X = input('Enter X please: ');
Y = input('Enter Y please: ');
Z = input('Enter Z please: ');
Cells={}
Cells{1}=zeros(X,Y);
for i=2:Y-1
Cells{i}=zeros(Y,Y);
end;
Cells{Y-1}=zeros(Y,Z);
You could do this without using cells, but I strongly advice you not to, so:
One way to do this, with each matrix being part of a cell:
dims = str2num(input('Type in selected x,y,z: ', 's'));
M = arrayfun(#(n) zeros(dims(n), dims(2)), [1 2*ones(1,y-1) 3], 'UniformOutput', 0)
%% In the command window:
Type in selected x,y,z: 3 4 2
M =
[3x4 double] [4x4 double] [2x4 double]
Note that with the str2num(input()) approach, you can input both: [4 3 2], [4, 3, 2], 4 3 2, 4, 3, 2 or even 4;3;2. It's basically impossible to make mistakes here!
The way this works is: arrayfun performs an operation for each elements of the vector [1 2*ones(1,y-1) 3]. The operation is to create a matrix of zeros, with the desired dimensions. UniformOutput is a parameter that must be set to false, or 0 if the output is something other than scalars.
To access, and make changes to any of the matrices:
When you type M{x}, you can think of that as the equivalent of just a matrix name, i.e. it's fine to use () to index the matrix, straight after the {}.
So, you can do:
M{1}(3,3) = 2;
which would assign the value 2 to the element (3,3) in matrix 1.
M1 = zeros(x,y);
M2 = zeros(y,y);
M3 = zeors(z,y);
Simple enough. Though why M2 and M3 in your question are the same I haven't figured out yet.
I have come across the following notation in MATLAB
y(:, :, :, 2) = y(:, :, :, 1);
y(:, 1, :, 4) = y(:, 1, :, 3);
y(:)?
Is there a general rule on : notation.
Thanks
All elements in that dimension. Example,
> A=[2, 3, 4; 0, 5, 7]
A =
2 3 4
0 5 7
> A(:, 2)
ans =
3
5
> A(1, :)
ans =
2 3 4
I'd like to point out that the actual question asked, "y(:)?", will give you the contents of matrix y reshaped to a Nx1 matrix (a.k.a. column vector).
This is a very convenient behavior because you are always guaranteed a column vector regardless of the input.
For example, given the following matrices:
a = [1,3;2,4];
b = [1,2,3,4];
c = [1;2;3;4];
a(:), b(:), and c(:) all give the following:
ans =
1
2
3
4
How can I partition a matrix into several smaller matrices to find unknown variables?
For example, given:
how can solve this problem by partitioning (splitting) a matrix 3x3 into smaller matrices (1x1 or other) to find the values of x, y, z and u?
Your matrix dimensions dont agree, or am I missing something?
Edit:
The code from Jeff E will work fine on smaller matrices.
For bigger matrices you will need to use backward substitution or some other algorithm, mainly because matrix inversion is a memory intensive task.
In the new image, you isolate for the unknown matrix using some identities:
A * X = B
(inv(A)) * A * X = (inv(A)) * B
I * X = (inv(A)) * B
X = (inv(A)) * B
In Matlab:
A = [1, 2; 0, 1]
B = [4, 7; 4, 6]
X = inv(A) * B
Output:
ans =
-4 -5
4 6
To solve an equation of the form A*X=B, you should use the backslash operator, since explicitly taking the inverse should be avoided if possible
A = [1, 2; 0, 1];
B = [4, 7; 4, 6];
X = A\B
X =
-4 -5
4 6