How to generate several matrix by varying one value?
조회 수: 1 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
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
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.
추가 답변 (1개)
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);
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!