How to generate several matrix by varying one value?

조회 수: 1 (최근 30일)
Redouane Ch
Redouane Ch 2018년 4월 25일
댓글: Redouane Ch 2018년 4월 29일
I'm using that code below, and I want to get 6 matrix, according to the value of "i". the problem is that I get only i=15. so just one value of "i", and then one results.
How can I force it to use all the values from i=10 till i=15
for i=10:15;
A= [i 0 0 30 ; 70 30 0 0 ; 30 70 0 0 ; 30 0 70 0];
B= [1; 2; 3; 4];
C = inv(A);
D= A\B;
end
E=bsxfun(@times,i,B)
i
Thank you

채택된 답변

Stephen23
Stephen23 2018년 4월 25일
편집: Stephen23 2018년 4월 25일
A = [0,0,0,30;70,30,0,0;30,70,0,0;30,0,70,0];
B = [1;2;3;4];
vec = 10:15;
out = cell(size(vec));
for k = 1:numel(vec)
A(1) = vec(k);
out{k} = A\B;
end
E = bsxfun(@times,vec,B)
Giving:
>> out{:}
ans =
0.012500
0.037500
0.051786
0.029167
ans =
0.012500
0.037500
0.051786
0.028750
ans =
0.012500
0.037500
0.051786
0.028333
ans =
0.012500
0.037500
0.051786
0.027917
ans =
0.012500
0.037500
0.051786
0.027500
ans =
0.012500
0.037500
0.051786
0.027083
  댓글 수: 5
Stephen23
Stephen23 2018년 4월 26일
@Redouane Ch: have a look at the code in my answer. Do you see how I define out before the loop, and within the loop allocate values to it using indexing? That is what you need to do too.
Redouane Ch
Redouane Ch 2018년 4월 29일
Thank you, it works with:
count=1;
for k = 1:numel(vec)
A(1) = vec(k);
out{k} = A\B;
Sola(count,:)=out{k};
count=count+1;
end
Solb=bsxfun(@times, vec', Sola);

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2018년 4월 25일
ii = 10:15;
A= [0 0 0 30 ;
70 30 0 0 ;
30 70 0 0 ;
30 0 70 0];
n = numel(ii);
A = repmat(A,1,1,numel(ii));
A(1,1,:) = ii;
s = size(A);
B= [1; 2; 3; 4];
D = zeros(numel(B),n);
C = zeros(s);
es = eye(s);
for jj = 1:n
D(:,jj) = A(:,:,jj)\B;
C(:,:,jj) = A(:,:,jj)\es;
end
E = bsxfun(@times,ii(:),B);
  댓글 수: 2
Redouane Ch
Redouane Ch 2018년 4월 25일
Thank you for your help
Redouane Ch
Redouane Ch 2018년 4월 25일
Actually, I couldn't use your code, seems there's a problem in
es = eye(s);

댓글을 달려면 로그인하십시오.

카테고리

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