need to do a string interpolation

조회 수: 17 (최근 30일)
Truman Cassady
Truman Cassady 2019년 3월 14일
답변: Star Strider 2019년 3월 14일
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')

답변 (1개)

Star Strider
Star Strider 2019년 3월 14일
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.

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by