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
Honglei Chen 2011 年 11 月 18 日

0 投票

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
Thomas 2011 年 11 月 18 日

0 投票

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
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
Andrei Bobrov 2011 年 11 月 18 日

0 投票

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
Image Analyst 2011 年 11 月 18 日

0 投票

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
kevin 2011 年 11 月 27 日
how would you do the same if it is a/b? also i dont find max part
Andrei Bobrov
Andrei Bobrov 2011 年 11 月 27 日
see my ADD
Image Analyst
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
Andrei Bobrov 2011 年 11 月 27 日
I am put this is
kevin
kevin 2011 年 11 月 27 日
if i do
a=[1:1:3]
b=[3:1:5]
c=a\b
i get
0 0 0
0 0 0
1.0000 1.3333 1.6667
if i do a/b i get c=0.52, both are not the answer i want, i want answer for 1/3,1/4,1/5,2/3... etc, how can i do that? thx
Image Analyst
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
kevin 2011 年 11 月 27 日
it didnt work, here is the code i used
a=[1:1:3]
b=[3:1:5]
bReciprocal =1./b
c=(a*bReciprocal)'
and i get
bReciprocal = 0.3333 0.2500 0.2000
??? Error using ==> mtimes
Inner matrix dimensions must agree.
is there any way to treat a and b as a range of number instead as a matrix?
kevin
kevin 2011 年 11 月 27 日
my actual question is more like a b and c is a number range from 1 to 10, d is range from 10 to 34)
and i try to f= (a/(b*c))*tan(d)

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeData Type Conversion についてさらに検索

質問済み:

2011 年 11 月 18 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by