mutiplying a role of number and seach or max and min value
조회 수: 3 (최근 30일)
이전 댓글 표시
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?
댓글 수: 0
답변 (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
댓글 수: 0
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));
댓글 수: 0
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!