mutiplying a role of number and seach or max and min value
이전 댓글 표시
let say a=[1:1:3] and b=[3:1:5]
and c = a*b
do i need to do anyting to make sure it multiply so it goes like 1*3 1*4 1*5 , 2*3 2*4 etc instead of 1*3 2*4 3*5?
and how do you find a and b when c is max or min?
답변 (4개)
Honglei Chen
2011년 11월 18일
You can use kron to do what you want.
kron(a,b)
doc kron
As to finding out what a and b is, given the regular structure in the product, once you know the index for max and min in c, it should be fairly easy to find out what the corresponding index in a and b. Using your example, let's say the index for max in c is Nc, then the index in a (Na) and b (Nb) can be calculated like
Na = fix(Nc-1,3)+1
Nb = Nc-(Na-1)*3
HTH
Thomas
2011년 11월 18일
Kron is a good way to do it,
However you could code multiple for loops and work it.. I hope this was not a homework problem..
c=[];
for a=
for b=
c1=a*b;
c=[c,c1];
if c1==max(c)
maxab=[a,b];
end
end
end
max(c)
maxab
댓글 수: 1
Jan
2011년 11월 18일
"close all; clear all" is useless here. See: http://www.mathworks.com/matlabcentral/answers/16484-good-programming-practice#answer_22301
Andrei Bobrov
2011년 11월 18일
a=1:3;
b=3:5;
c = a(:)*b(:).';
[~, a2] = cellfun(@(x)x(c(:)),{@min,@max});
[iout,jout] = ind2sub(size(c),a2);
c_min = [a(iout(1)) b(jout(1))]
c_max = [a(iout(2)) b(jout(2))]
ADD 19:01MDT 27.11.2011
out = bsxfun(@rdivide,a.',b)
or (20:14MDT 27 Nov 2011)
out = a.'*(1./b)
[~, a3] = cellfun(@(x)x(out(:)),{@min,@max});
idx_a = rem(a3-1,numel(a))+1; % for min and max of a/b
idx_b = ceil(a3/numel(a));
Image Analyst
2011년 11월 18일
Here, run this code and I think you'll see what you're trying to figure out:
a=[1:1:3] % Row vector using your strange syntax.
b=[3:1:5] % Another row vector.
c1 = a' * b % Note transpose operator '
c2 = a .* b % Note dot
From your description, it sounds like you're wanting to make sure you get c1 and not c2. Results:
a =
1 2 3
b =
3 4 5
c1 =
3 4 5
6 8 10
9 12 15
c2 =
3 8 15
Now, for the max part:
% Find max of c1
cMax = c1 == max(c1(:))
% Find row(s) and columns(s) where c1 = cMax.
[rowsOfMax colsOfMax] = find(cMax)
% Extract out a and b where c == cMax.
aAtCMax = a(rowsOfMax)
bAtCMax = b(colsOfMax)
Do similar for the min of c1.
댓글 수: 8
kevin
2011년 11월 27일
Andrei Bobrov
2011년 11월 27일
see my ADD
Image Analyst
2011년 11월 27일
kevin, the max part was the code following where it says "Now, for the max part." Use / instead of * for division or matrix inverting.
andrei: How did that ADD and the time stamp get in there? Sometimes I see things like that or that say EDIT. Did you put that there or does it add it automatically? I never see anything with my code when I modify it.
Andrei Bobrov
2011년 11월 27일
I am put this is
kevin
2011년 11월 27일
Image Analyst
2011년 11월 27일
Like this:
bReciprocal = 1 ./ b
c= (a * bReciprocal)'
c =
0.3333 0.6667 1.0000
0.2500 0.5000 0.7500
0.2000 0.4000 0.6000
kevin
2011년 11월 27일
kevin
2011년 11월 27일
카테고리
도움말 센터 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!