Permutation of a vector and its sub elements using MATLAB [closed] - matlab

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have the vector below:
v={'T','AT','AS','C'};
I would like to see all the possible permutations for this vector. To do so I can use the command below:
p=perms(v)
But I want to go one step further as each of the elements has sub index of 1 to 4, for example, T1,T2,T3,T4 .....C1,C2,C3,C4. I would like to have all the possible permutations with its sub index as see such results
T1,AT1,AS1,C1
C3,AT3,AS3,t3
AS2,AT2,C2,T2
.
.
.
Could you please help me how to do that?
Thanks

You can do this by first using ndgrid to generate a set of indices for all your possible combinations:
v = {'T1', 'AT1', 'AS1', 'C1'; ...
'T2', 'AT2', 'AS2', 'C2'; ...
'T3', 'AT3', 'AS3', 'C3'; ...
'T4', 'AT4', 'AS4', 'C4'};
[ind1, ind2, ind3, ind4] = ndgrid(1:4);
c = [v(ind1(:), 1) v(ind2(:), 2) v(ind3(:), 3) v(ind4(:), 4)];
And c will be a 256-by-4 cell array, as expected (44 combinations). Now you can expand each row by it's total number of permutations using perms like so:
p = perms(1:4);
p = reshape(c(:, p.').', 4, []).';
And p will be a 6144-by-4 cell array, also as expected (24 permutations times 256 combinations).

Related

how to convert Flutter List<List> to List [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 months ago.
The community is reviewing whether to reopen this question as of 8 months ago.
Improve this question
// Here Is my Code
List data = [[0,1],[0,1],[0,1]];
List requiredList = [1,1,1];
To get the second value of the elements in the first list as second list you can do this:
List requiredList = data.expand((e)=>[e[1]]).toList();
Simply loop through the data and add the item in index 1 into output.
void main() async {
List data = [[0,1],[0,1],[0,1]];
List output = [];
for(final d in data){
output.add(d[1]);
}
print(output);
}
Output:
[1, 1, 1]
Your data contains lists, i.e to access data of list you have to pass index of list, i.e 0 1 2 etc.
List data = [[0,1],[0,1],[0,1]];
^ ^ ^
| | |
0 1 2
Above mentioned are index of each sub-list
So if you want to access data from sub-list, call the main list followed by index of sub-list and index number of elements of sub-list.
data[0][1] //this will give an output of 1
^ ^ ^
|. |. |
*. **. ***
*. => list containing lists here
** => Index of sub-list
*** => index of element in list
Check this video here to get more clarity of concept. I've attached a video related to python but same things apply in dart/flutter as well.

How to compute the mean-square in Matlab? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to compute the square of mean-square for each element of l
l=[0.02817088, -0.74100320, -0.54062120, -0.24612808, 0.06945337, -0.58415690, -0.51238549,
-0.07862326, -0.42417337, -0.33482340, -0.21339753, -0.03890844, -0.59325371, 0.28154593,
-0.32133359,-0.13534792, 0.14060645, 0.32204972, 0.44438052, -0.21750973,-0.59107599,
-0.60809913]'
k= -0.2224834
sum(l-k)^2/22
I am not sure if sum(l-k)^2/22 is the sum of each (l[j]-k) for j=1,2,...,22?
ans = 2.4223e-14
I guess what you need might be
>> mean((l-k).^2)
ans = 0.10945
Data (You need ... for line continuation if you have data in different lines for l)
l=[0.02817088, -0.74100320, -0.54062120, -0.24612808, 0.06945337, -0.58415690, -0.51238549, ...
-0.07862326, -0.42417337, -0.33482340, -0.21339753, -0.03890844, -0.59325371, 0.28154593, ...
-0.32133359,-0.13534792, 0.14060645, 0.32204972, 0.44438052, -0.21750973,-0.59107599, ...
-0.60809913]'
k= -0.2224834

Remove specific integers from matrix [duplicate]

This question already has answers here:
Exclude elements from array [duplicate]
(3 answers)
Closed 6 years ago.
I have a matrix <1x1000> containing integers. It contain the value 150 a couple of times and I want to remove that value completely. Any ideas how to?
Help is much appreciated!
If you want to remove all elements that equal 150 then
M = M(M ~= 150)
If you want to remove all elements belonging to a list of undesired numbers then
list = [150, 230, 420]
M = M(~ismember(M, list))
Same but different expression
M(M==150)=[];
list = [150,230,420];
M(ismember(M,list))=[];
When you type A(index)=[], it delete A(index). For example,
A = [1,2,3];
A(2) = [];
Then
A = [1,3]

How many have a name longer than 10 characters in xlsx matlab [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How many elements have a name longer than 10 characters in xlsx matlab?
I tried this.
[number cities] = xlsread('weather.xlsx', 'city')
if cities{1}<=char(10);
x+1=x;
Your code above only checks to see if the first city has less than 10 characters. Also, char(10) does not make any sense. You are checking to see if the string contained in cities{1} is less than or equal to the character array containing 10.
Because cities is in a cell array, I would use cellfun to first return the length of each city. Then you can use a sum as well as a Boolean condition to help you figure out how many there are that have more than 10 characters.
As such, here is the code:
A = cellfun(#length, cities);
numCitiesMoreThan10 = sum(A > 10);
Here is an example (simulated):
cities = {'New York', 'Los Angeles', 'Toronto', 'Ottawa', 'Sydney', 'Melbourne', ...
'Timbuktu', 'Singapore', 'Mississippi'};
A = cellfun(#length, cities);
numCitiesMoreThan10 = sum(A > 10);
>> numCitiesMoreThan10 =
2
This makes sense, as the only two cities with more than 10 characters (including spaces) are Los Angeles and Mississippi.
Aside
Just learned that cellfun has something built-in that can do this. You can also do:
A = cellfun('size', cities, 2);
This accesses each element in the cell array (cities), and returns the size of whatever dimension you specify in the last parameter of cellfun when you call it with the size parameter. You can also chain more than one cell array together. The reason why you are choosing the third parameter as 2 is because each string in the cell array is a 1 x N array. As such, we need to read how many columns there are so that this equates to the length of each string.

How to add or multiply digits of a number [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
As the title says i want to know if its possible to add or multiply digits of a number in matlab
eg:
for a number
123456789
add the digits, that is
1+2+3+4+5+6+7+8+9 =>45 =>4+5 => 9
advance thanks for your help
Numeric approach
A = 35356536576821;
A = abs(A);
xp = ceil(log10(A)):-1:1;
while ~isscalar(xp)
A = sum(fix(mod(A,10.^xp)./10.^[xp(2:end) 0]));
xp = ceil(log10(A)):-1:1;
end
Char approach
A = '35356536576821';
A = char(regexp(A,'\d+','match'));
while ~isscalar(A)
A = num2str(sum(A - '0'));
end
Both, first take the absolute number (strip the minus) then: the numeric one counts with log10() how many digits a number has and through modulus and divisions extracts the digits which are summed, while the char approach convert to numeric digits with implicit conversion of - '0', sums and converts back to string again.
Both approaches might suffer from floating point approximation, but the numeric one is definitely more exposed to it:
A = 11111111111111111;
xp = ceil(log10(A)):-1:1;
fix(mod(A,10.^xp)./10.^[xp(2:end) 0])
ans =
Columns 1 through 13
1 1 1 1 1 1 1 1 1 1 1 1 1
Columns 14 through 17
1 1 1 2
To have approximations with the char approach, the first sum should exceed a 16 digits number.