Looping through 3D matrix
5 次查看(过去 30 天)
显示 更早的评论
Hi, I'm trying to populate a 3D matrix of zeros (3x14x12), nextvoy with a subset of rows in another matrix, trade, according to a lookup table, desigcty. The matrices look like this: trade =
682
682
682
566
566
566
579
579
579
desigcty =
682
579
566
>> tmp_x
tmp_x =
1
1
1
2
2
2
3
3
3
4
4
4
For example, nextvoy(:,:,1) should have all the entries of 682, then nextvoy(:,:,2) has 579, and then 566 for nextvoy(:,:,3). Then it should repeat this for the next 3 i's in nextvoy and so on.
The code I have overwrites each array in nextvoy with 566 because it's the last j (it's not performing according to expectations):
for i = 1:(size(desigcty,1)*nx); %for all load ports and ships: (:,:,i)
for j = 1:size(desigcty,1); %loop through each load port (j,:,:)- 3 for now
nextvoy(:,1,i)= tmp_x(i,1); %ship ID
[r1,~,~]= find(trade(:,1)==desigcty(j,1),1, 'first');
[r2,~,~]= find(trade(:,1)==desigcty(j,1),1, 'last');
nextvoy(:,2:9,i)= trade(r1:r2,1:8); %copies subset of trade matrix according to load code
end;
end;
For example, part of the sequence this code produces is >> nextvoy(:,1:2,1:6)
ans(:,:,1) =
1 566
1 566
1 566
ans(:,:,2) =
1 566
1 566
1 566
ans(:,:,3) =
1 566
1 566
1 566
ans(:,:,4) =
2 566
2 566
2 566
ans(:,:,5) =
2 566
2 566
2 566
ans(:,:,6) =
2 566
2 566
2 566
But I need: >> nextvoy(:,1:2,1:3)
ans(:,:,1) =
1 682
1 682
1 682
ans(:,:,2) =
1 579
1 579
1 579
ans(:,:,3) =
1 566
1 566
1 566
ans(:,:,4) =
2 682
2 682
2 682
ans(:,:,5) =
2 579
2 579
2 579
ans(:,:,6) =
2 566
2 566
2 566
And so on...
3 个评论
Jan
2011-11-21
@Sophia: again, please format the code in your original message. You only have to press the "edit" button, mark the text with the mouse and press the "{} code" button. It is really worth to spend the necessary 4 seconds to make it easier for the readers.
I still do not get the point iof the question. Does the code do what you want? Do you get an error message? Does the result differ from your expectations - if so, how?
It would be much easier to answer, if you post running code. Currently I cannot copy&paster the definitions of trade and desigcty. But I want to concentrate on producing an answer, not on finding out, what the question could be.
采纳的回答
Andrei Bobrov
2011-11-21
in your case
a1 = sortrows(tride);
nextvoy = permute(reshape(a1,3,3,[]),[1 3 2]);
variant 2
a1 = sortrows(tride);
a2 = sort(desigcty(:,1));
[b,n] = histc(a1(:,1),a2);
nextvoy = zeros(max(b),14,numel(b));
m = size(tride,2);
for i1 = 1:numel(b)
nextvoy(1:b(i1),1:m,i1) = a1(n==i1,:);
end
EDIT variant
[c,idx] = sort(desigcty(:,1));
[b,n] = histc(tride(:,1),c);
nextvoy = [permute(ones(max(b),1)*tmp_x(:)',[1 3 2]),zeros(max(b),13,numel(tmp_x))];
k = size(tride,2);
p0 = numel(unique(tmp_x));
p = numel(tmp_x)/p0;
for i1 = 1:numel(b)
i2 = idx(i1);
nextvoy(1:b(i2),2:k+1,i1:p:end) = repmat(tride(i2==n,:),[1,1,p0]);
end
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!