Transformation of the Matrix in a Loop
Show older comments
Hello Everyone,
I have the measurement mapping field which looks has dimensions 100x100 as shown

Now i want to turn this mapping to a column matrix of 10000x 3 like shown in the figure

How would i be able to do it. I tried for loop but the result is only 100x3
my code is something like zhis
i=1;
for i=1:109
D=Z(:,i)
your_result = [X,Y,D]
end
I want this loop to keep adding values but couldnt able to work it out. Any assistance would be highly appreciable.
Regards,
Irfan Tahir
Accepted Answer
More Answers (1)
Jon
on 7 Oct 2019
Sorry, I inadvertently deleted my earlier response.
So I'm assuming you want an array with
y1 x1 z11
y2 x2 z21
y3 x3 z31
.
.
.
y1 x2 z12
y2 x2 z22
.
.
.
This isn't the way you have it in your original question but maybe you just didn't type it correctly.
It could probably be vectorized (avoid a loop) but here is how you could do it with a loop
[numRows,numCols] = size(Z)
Ztbl = zeros(numRows*numCols,3)
row = 1
for j = 1:numCols
for i = 1:numRows
Ztbl(row,:) = [y(i) x(j) Z(i,j)]
row = row+1
end
end
2 Comments
Jon
on 7 Oct 2019
You can also vectorize it and avoid loops using
Ztbl = [repmat(y(:),numCols,1) repelem(x(:),numRows) Z(:)]
Irfan Tahir
on 9 Oct 2019
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!