using repmat instead of for loop
조회 수: 6 (최근 30일)
이전 댓글 표시
I have a 1x24 matrix A and a 2000x24 matrix D
I want to take column 1 in A and multiply it by all the rows in column 1 of D, then take the second column of A and multiply it by all the rows in column 2 of D and do that for all 24 columns without using a for loop.
A coworker suggested:
G = D .* repmat(A(1:channels), size(D,1),1);
Hoping G is a 2000x24 matrix. It is, but its not doing what i want it to. Any suggestions?
댓글 수: 0
답변 (3개)
the cyclist
2011년 10월 13일
That should work. But even better is:
>> G = bsxfun(@times,A,D);
If that does not give the answer you expect, I think you need to be a little bit more detailed about what you want, possibly with a small explicit example.
댓글 수: 0
Mason
2011년 10월 13일
댓글 수: 2
the cyclist
2011년 10월 13일
You had errors in both figures 4 and 5. In both cases, you used "shift" instead of "shift-1". In figure 5, you also multiplied by the shift instead of adding. I think you may also have multiplied instead of divided in one place, but I started to get confused, so I basically went back to your figure 1 and 2 code and translated it directly, rather than starting from your figure 4 and 5 code.
I believe the following replicates what you want.
figure(4)
Shifted = bsxfun(@plus,d,shift*(0:channels-1));
plot(Shifted, 'b');% plots pre-normalized values
view(90,90)
figure(5)
normalized = highest_values / highest_value; % matrix of percentages each channel peak value is to the max amplitude value
normalized_signals = bsxfun(@rdivide,d,normalized);% normalized signals on all 24 channels to be plotted
normalized_signals2 = normalized_signals / highest_value;% normalized signals on all 24 channels for imagesc
shifted = bsxfun(@plus,normalized_signals,shift*(0:channels-1))
plot(shifted,'b'); % plots normalized values shifted
view(90,90)
Jan
2011년 10월 13일
For the "clear all" see: http://www.mathworks.com/matlabcentral/answers/16484-good-programming-practice#answer_22301
Mason
2011년 10월 13일
댓글 수: 1
the cyclist
2011년 10월 13일
Glad to help. Next time, rather than adding your own "Answer" as a response, try using the "Comment on this Answer" feature. As this thread currently stands, it is a little bit tricky for you to accept my answer as being helpful, because that help is spread out.
참고 항목
카테고리
Help Center 및 File Exchange에서 Measurements and Spatial Audio에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!