Matrix to string in Matlab - arrays

I have an m*6 matrix in matlab and I want to display it in a string without the whitespace and the semicolons. I used the mat2str function but the output would e like that [1 2 3; 4 5 6; ...] . Is there any function or effecient way to prduce a string with no whitespace and semicolons ?
Kind Regards,

str = sprintf('%d', mtx);

Related

Decimal to binary conversion in fortran

I am new in Fortran so I can't really evaluate where the semantic mistake is. From what I see the syntax is ok and when I build I don't see any mistake from the compiler I have in "Simply Fortran" I didn't find any standard function for binary conversion and I think there aren't any from what I see. Can anyone help me to fix the code so that I got displayed the elements of the array and the respective element in binary ? Thank you in advance for your help.
I think like this it works now.
I initialised the array with given numbers though.
code:
program array_binary
implicit none
integer, dimension(8) :: numbers
character(len=32), dimension(8) :: binary_numbers
integer :: i, j, k
numbers = [8, 9, 3, 4, 89, 6, 7, 8]
do i = 1, 8
write(*,*) numbers(i)
end do
do i = 1, 8
k = numbers(i)
binary_numbers(i) = ""
do j = 1, 32
if (mod(k,2) == 0) then
binary_numbers(i) = "0" // binary_numbers(i)
else
binary_numbers(i) = "1" // binary_numbers(i)
end if
k = k / 2
end do
end do
do i = 1, 8
write(*,*) binary_numbers(i)
end do
end program array_binary
I won't give you the solution, as it's better if you get it by yourself. However it looks like you have troubles with how arrays of strings work. Here is how:
character(len=32) :: string is a 32 characters string. You can access substrings with the : notation
string = "abcdef"
write(*,*) string ! full string
write(*,*) string(3:5) ! 3 characters substring
write(*,*) string(2:2) ! 1 character substring
! note that string(2) gives an error
string(1:3) = "ABC" ! write in a subtring
write(*,*) string
results in:
abcdef
cde
b
ABCdef
Now the arrays of string:
character(len=32) :: string(8) is a 8 elements arrays, each element being a 32 characters string. This is equivalent to
character(len=32), dimension(8) :: string
When indexing arrays of strings, the first index level refers to the array elements, and the second index level refers to the subtrings:
string(2) is the 2nd element of the array, and is a full 32 characters string. It is equivalent to string(2)(:)
string(2)(10:16) is a 7 characters substring of the 2nd element of the array
string(:)(10:10) is a 8 elements array of 1 character subtrings
string(4:6) is a 3 elements subarray of 32 characters strings

String to array in MATLAB

I'm trying to convert an input string "[f1 f2]", where both f1 and f2 are integers, to an array of two integers [f1 f2]. How can I do this?
I have found a way by using sscanf:
f = sscanf(s, "[%d %d]", [1 2]);
where s is the array-like string and f the new array of integers.
You can just use str2num:
f = str2num( "[123 456]" )
% f = [123, 456]
You can use indexing together with strsplit()
my_str = "[32 523]";
split_str = strsplit(my_str, ' '); % split on the whitespace
% The first cell contains "[32" the second "523]"
my_array = [str2num(split_str{1}(2:end)) str2num(split_str{1}(1:end-1))]
my_array =
32 523
When you have more numbers in your string array, you can lift out the first and last elements of split_str out separately (because of the square brackets) and loop/cellfun() over the other entries with str2num.

Matlab: Extract LxN array from LxMxN array

Suppose x=zeros(L,M,N). For a fixed component, the remaining array is basically a matrix. So I should be able to do something like y = x(:,2,:). Then, I expect y to be a matrix, i.e. an LxN array. But I instead get a Lx1xN array.
How can I obtain a standard matrix from a three-dimensional array, after I fixed one component? I use matlab.
Use permute to rearrange the dimensions after indexing:
x = zeros(2,3,4); % L×M×N
y = permute(x(:,2,:), [1 3 2]); % move 2nd dimension to 3rd
The code sends the second dimension to the end. This transforms the L×1×N array into an L×N×1 array, which is the same as an L×N matrix, because trailing singleton dimensions are ignored; in fact, arrays can be considered to have an infinite number of trailing singleton dimensions. As a check,
>> size(y)
ans =
2 4
A word of caution: some people may be tempted to use the simpler y = squeeze(x(:,2,:)), but that squeezes all (non-trailing) singleton dimensions, not just the second, and so it gives a wrong result for L=1.
You can use reshape:
y = reshape(x(:,2,:), [L N]);

How to pass two dimensional array to a function in F#?

I am trying to create a function in F# that takes as input a two dimensional array of integers (9 by 9), and prints its content afterwards. The following code shows what I have done :
let printMatrix matrix=
for i in 0 .. 8 do
for j in 0 .. 8 do
printf "%d " matrix.[i,j]
printf "\n"
The problem is that F# does not automatically infer the type of the matrix, and it gives me the following error: "The operator 'expr.[idx]' has been used an object of indeterminate type based on information prior to this program point. Consider adding further type constraints".
I tried to use type annotation in the definition of the function, but I think I did it wrong. Any idea how I can overcome this issue?
Change it to
let printMatrix (matrix:int [,])=
for i in 0 .. 8 do
for j in 0 .. 8 do
printf "%d " matrix.[i,j]
printf "\n"
This is due to how the F# type infrence algorithm works
The type inference algorithm doesn't really likes the bracket operator, because it can't guess which type is the object.
A workaround would be to give the matrix to a function that the compiler knows the type, in that example, Array2D.get does the same thing as the bracket operator. It knows it's an int matrix because of the "%d" on the printf
let printMatrix matrix =
for i in 0..8 do
for j in 0..8 do
printf "%d" <| Array2D.get matrix i j
printf "\n"

Values of Variables Matrix NumPy

I'm working on a program that determines if lines intersect. I'm using matrices to do this. I understand all the math concepts, but I'm new to Python and NumPy.
I want to add my slope variables and yint variables to a new matrix. They are all floats. I can't seem to figure out the correct format for entering them. Here's an example:
import numpy as np
x = 2
y = 5
w = 9
z = 12
I understand that if I were to just be entering the raw numbers, it would look something like this:
matr = np.matrix('2 5; 9 12')
My goal, though, is to enter the variable names instead of the ints.
You can do:
M = np.matrix([[x, y], [w, z]])
# or
A = np.array([[x, y], [w, z]])
I included the array as well because I would suggest using arrays instead of of matrices. Though matrices seem like a good idea at first (or at least they did for me), imo you'll avoid a lot of headache by using arrays. Here's a comparison of the two that will help you decide which is right for you.
The only disadvantage of arrays that I can think of is that matrix multiply operations are not as pretty:
# With an array the matrix multiply like this
matrix_product = array.dot(vector)
# With a matrix it look like this
matrix_product = matrix * vector
Can you just format the string like this?:
import numpy as np
x = 2
y = 5
w = 9
z = 12
matr = np.matrix('%s %s; %s %s' % (x, y, w, z))

Resources