How to generate several matrix by varying one value?

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

 Accepted Answer

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 Comments

Thank you for your help, but still whene I use
Sol=bsxfun(@times, vec, out{k});
we get only an operation with the last cell, from out{k}, not all of them. How can we operat in all of them?
"How can we operat in all of them?"
Move that operation inside the loop. Having the operation inside the loop would means that on each iteration you could perform that calculation. That is what loops do.
with
for k = 1:numel(vec)
A(1) = vec(k);
out{k} = A\B;
Sol=bsxfun(@times, vec, out{k});
end
I get
Sol =
0.1250 0.1375 0.1500 0.1625 0.1750 0.1875
0.3750 0.4125 0.4500 0.4875 0.5250 0.5625
0.5179 0.5696 0.6214 0.6732 0.7250 0.7768
0.2708 0.2979 0.3250 0.3521 0.3792 0.4062
and that's using one cell only
@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.
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);

Sign in to comment.

More Answers (1)

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 Comments

Thank you for your help
Actually, I couldn't use your code, seems there's a problem in
es = eye(s);

Sign in to comment.

Categories

Find more on MATLAB 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!