필터 지우기
필터 지우기

Uneven data multiplication in Matlab

조회 수: 1 (최근 30일)
Neesha
Neesha 2014년 6월 17일
답변: Andrei Bobrov 2014년 6월 18일
I have following
Data.numberOfDishes
Data.numOfLocations
As follows
numberOfDishes numberOfLocations
3 [1, 2, 4]
4 [9, 8, 5]
6 [11, 1, 9]
7 [2, 3, 4]
9 [7, 7, 7]
I want to have resulting column 'total' by doing numberOfDishes/each element of numberOfLocations
For example, so that 'total' for first row would be [3/1, 3/2, 3/4]
i know '.' would not work. mrdivide does not seem an option. Can i do this without having to create numberOfDishes of the same size as numOfLocations

채택된 답변

Star Strider
Star Strider 2014년 6월 17일
This works:
NumberOfDishes = [3 4 6 7 9];
NumberOfLocations = [1, 2, 4; 9, 8, 5; 11, 1, 9; 2, 3, 4; 7, 7, 7];
for k1 = 1:length(NumberOfDishes)
Total(k1,:) = NumberOfDishes(k1)./NumberOfLocations(k1,:);
end
% To express them as fractions:
Total = rats(Total)
  댓글 수: 4
Image Analyst
Image Analyst 2014년 6월 18일
Let's compare the "vectorized" approach with the brute force "for loop" approach:
NumberOfDishes = [3; 4; 6; 7; 9]
NumberOfLocations = [1, 2, 4; 9, 8, 5; 11, 1, 9; 2, 3, 4; 7, 7, 7]
tic
for k1 = 1:length(NumberOfDishes)
Total(k1,:) = NumberOfDishes(k1)./NumberOfLocations(k1,:);
end
toc
% To express them as fractions:
Total
Total = rats(Total)
% Use a "vectorized" approach.
tic
t = repmat(NumberOfDishes(:), [1, size(NumberOfLocations, 2)]) ./ NumberOfLocations
toc
Results in command window:
Elapsed time is 0.000032 seconds.
Elapsed time is 0.000402 seconds.
Hmmmm.... Looks like the for loop is 12 times faster.
Star Strider
Star Strider 2014년 6월 18일
Interesting!
I think the delay with the vectorised approach is the overhead in repmat.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2014년 6월 18일
out = bsxfun(@rdivide,NumberOfDishes,NumberOfLocations);

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by