need to do a string interpolation
8 views (last 30 days)
Show older comments
I have six matrices with the same name except a number at the end to distinguish them. I need to access the same element of each one. How can I write logic that would access a matrix but change the last character after each iteration.
unction [] = staticstability()
for k = 1:length(LongLTI)
if LongLTI + k+(1,1)<=0
fprintf('From Table 4.1, the aircraft does have static stability for x_body force with respect to forward speed. \n')
else
fprintf('From Table 4.1, the aircraft does not have static stability for x_body force with respect to forward speed. \n')
0 Comments
Answers (1)
Star Strider
on 14 Mar 2019
‘How can I write logic that would access a matrix but change the last character after each iteration.’
No need.
I would concatenate them, and address the same element of each ‘page’ of the concatenated array.
If they are not all the same size, concatenate them as cells:
matrix1 = randi(9, 3, 4);
matrix2 = randi(9, 3, 5);
matrix3 = randi(9, 4, 4);
matrixc = cat(3, {matrix1},{matrix2},{matrix3});
same_element1 = cellfun(@(x)x(2,3), matrixc, 'Uni',0);
same_elementd = squeeze([same_element1])
If they are all the same size, this is even easier:
matrix4 = randi(9, 5, 5);
matrix5 = randi(9, 5, 5);
matrix6 = randi(9, 5, 5);
matrixd = cat(3, matrix4, matrix5, matrix6);
same_element2 = squeeze(matrixd(2,3,:))
Make appropriate changes for your matrices.
This eliminates the problem of creating code that addresses them as dynamic arrays.
0 Comments
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!