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.
Related
I need to create an empty array of matrices, and after fill it with matrices of the same size.
I have made a little script to explain:
result = [];
for i = 0: 4;
M = i * ones(5,5); % create matrice
result = [result,M]; % this would have to append M to results
end
Here result is a matrix of size 5*25 and I need an array of matrices 5*5*4.
I have been researched but I only found this line: result = [result(1),M];
The issue is that [] implicitly concatenates values horizontally (the second dimension). In your case, you want to concatenate them along the third dimension so you could use cat.
result = cat(3, result, M);
But a better way to do it would be to actually pre-allocate your result array using zeros
result = zeros(5, 5, 4);
And then within your loop fill each "slice" of the 3D array with the values.
for k = 0:4
M = k * ones(5,5);
result(:,:,k+1) = M;
end
Pretty simple problem, I want to create an array with char in a for loop.
code:
a = [1:5];
arr = [];
for i = 1:length(a)
arr(i) = ['f_',num2str(i)]
end
I am getting error:
In an assignment A(I) = B, the number of elements in B and I must be the same.
all i want is an array:
[f_1 f_2 f_3....]
This is because arr(i) is a single element, while ['f:', num2str(i)] contain three characters. Also, for i = 1:length(1) doesn't really make sense, since length(1) is guaranteed to be 1. I guess you wanted for i = 1:length(a). If that's the case I suggest you substitute length with numel and i with ii.
The better way to create the array you want is using sprintf like this:
sprintf('f_%i\n',1:5)
ans =
f_1
f_2
f_3
f_4
f_5
Or possiblby:
sprintf('f_%i ',1:5)
ans =
f_1 f_2 f_3 f_4 f_5
I guess this is what you really wanted:
for ii = 1:5
arr{ii} = ['f_', num2str(ii)];
end
arr =
'f_1' 'f_2' 'f_3' 'f_4' 'f_5'
Or simpler:
arr = arrayfun(#(n) sprintf('f_%i', n), 1:5, 'UniformOutput', false)
The last two can be indexed as follows:
arr{1}
ans =
f_1
You can also do (same result):
str = sprintf('f_%i\n', 1:5);
arr = strsplit(str(1:end-1), '\n')
If you're doing this to create variable names, then please don't. Use cells or structs instead.
I have 3 20x2 double arrays A, B and C. I want to combine them in one 3d array D so that D(:,:,1) will return A, D(:,:,2) will return B and D(:,:,3) will return C.
Using cat to concatenate along the third dimension might be the elegant way -
D = cat(3,A,B,C)
Here, the first input argument 3 specifies the dimension along which the concatenation is to be performed.
Like this?
A = 1*ones(20,2);
B = 2*ones(20,2);
C = 3*ones(20,2);
D = zeros(20,2,3); % Preallocate the D Matrix
D(:,:,1) = A;
D(:,:,2) = B;
D(:,:,3) = C;
D(1,1,1) % prints 1
D(1,1,2) % prints 2
D(1,1,3) % prints 3
I'm trying decode an array of 1's and 0's using variable length coding. For example, if the string is [1 0 1 1], and A = [1 0] and B = [1 1], my program should give me a string something like: ['A', 'B'].
I first created an empty character array x = repmat(char(0),1,10)
But now when I detect a code word using a for loop and if statements, how do I add the character to this array x? Would it display the characters in the decoded string?
First of all, pre-defining the length of x is unnecessary in MATLAB because the language allows you resize arrays on-the-fly. That being said, preallocation is a sometimes a good idea because it will run faster.
Assuming you want to preallocate the length of x, you can assign a character to an element in x directly:
% Preallocate x
x = repmat(char(0),1,10);
% Assign a character to x
x(1) = 'A';
Where you can replace the 1 with any element in the array.
The challenge with this is that you need to keep track of where you are at in this preallocated array. If you already wrote characters to positions 1, 2, and 3, you need to know that the next assignment will write to the 4th element of x: x(4) = ....
A more elegant solution might be the following:
x = [];
if foundLetter
x(end) = 'A';
end
This adds the letter A to the end of the pre-defined character array x. It doesn't require that you preallocate the length of x.
You can index the character array x just as you would an array of doubles.
x(1) = 'A'; %Assign the char 'A' to the first element of x
.
.
.
x(10) = 'B'; %Assign the char 'B' to the tenth element of x
Here is a short example of what you would like to do.
clear decodedMsg
% define a dictionary between codes and corresponding characters
code{1,1} = 'A'; code{1,2} = '11';
code{2,1} = 'B'; code{2,2} = '101';
code{3,1} = 'C'; code{3,2} = '100';
% declare a sample message, corresponds to ABCBA
msg = [1 1 1 0 1 1 0 0 1 0 1 1 1];
%keeps track of the number of matches found, used to add to decodedMsg
counter = 1;
% get length of message and use to iterate through the msg
N = length(msg);
buffer = []; %must declare buffer if you are to use (end + 1) indexing later on
for i = 1:N
buffer(end + 1) = msg(i); %add the next msg value to the buffer
strBuf = strrep(num2str(buffer),' ',''); %convert buffer into string, e.x. [1 0 1] => '101'
findMatch = ismember(code(:,2),strBuf); %findMatch contains a 1 if a match is found
if any(findMatch) %if any element in findMatch is 1
decodedMsg(counter) = code{findMatch,1};%use that element to index a char in code cell array
counter = counter + 1; %increment counter since another code was found
buffer = []; %reset buffer for next string of 1s and 0s
end
end
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);