Matrix Size Changing in a loop

1 次查看(过去 30 天)
Burak
Burak 2017-3-26
评论: KSSV 2017-3-27
Hi, I need to calculate a matrix say A(v,n,m) like this code
for n=1:5
m=-n:n;
for v=1:5
for i=1:numel(m)
A(v,n,i)= % my function
end
end
end
now A is a 5x5x11 matrix, but I want to calculate A as a changing matrix size like for m=1 , A=10x10 for m=2 A=9x9, for m=3 A=8x8, same as for negative values of m. Because I need to inverse of A, and I can't inverse for all m values if I calculate like my code. How can I do that , I don't want to calculate it separately. Thanks for any help.
  3 个评论
Joshua
Joshua 2017-3-27
From what I understand, you want to create a 3D array which holds matrices of different sizes in the 3rd dimension. Then, you want to invert each of these matrices ignoring the extra zeros. I think I found a solution.
clear
clc
num=11;
m=-3:3;
for i=1:numel(m)
for v=1:num-m(i)
for n=1:num-m(i)
A(v,n,i)= rand(1);
end
end
Ai(1:num-m(i),1:num-m(i),i)=inv(A(1:num-m(i),1:num-m(i),i));
end
num is an arbitrary scaling variable (so m(i)=1 makes 10x10, m(i)=2 makes 9x9, etc.). Also, I replaced your function with the rand function because your function was making non-invertible matrices every time. Take a look at matrices A and Ai and see if it what you are looking for.
KSSV
KSSV 2017-3-27
With m=-n:n; the indices in matrix A(v,n,i) will be negative and your code stops popping out a error. You are not clear with your question.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by