MATLAB Answers

0

Transformation of the Matrix in a Loop

Irfan Tahir さんによって質問されました 2019 年 10 月 7 日
最新アクティビティ Irfan Tahir さんによって コメントされました 2019 年 10 月 9 日
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
b.JPG
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

  0 件のコメント

サインイン to comment.

2 件の回答

Guillaume
回答者: Guillaume
2019 年 10 月 7 日
 採用された回答

With your invented notation it's difficult to know what you actually have in matlab.
Assuming you have a matrix such as:
Z = [NaN 1 1.5 2 2.5 3
1 0.2 0.3 0.4 0.5 0.6
1.2 0.3 0.9 1.2 1.5 1.8
1.6 0.4 1.2 1.6 2.0 2.4
1.8 0.5 1.5 2.0 2.5 3.0]
Obtaining your desired result is easy and no loop is needed:
[Y, X] = ndgrid(Z(2:end, 1), Z(1, 2:end));
D = reshape(cat(3, X, Y, Z(2:end, 2:end)), [], 3)

  1 件のコメント

Irfan Tahir 2019 年 10 月 7 日
yes, it worked, thanks alot. The software we uses is Uniplot and it doesnt plot like Matlab do. So needed the data in colum form.
Thanks alot

サインイン to comment.


Jon
回答者: Jon
2019 年 10 月 7 日

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 件のコメント

Jon
2019 年 10 月 7 日
You can also vectorize it and avoid loops using
Ztbl = [repmat(y(:),numCols,1) repelem(x(:),numRows) Z(:)]
Irfan Tahir 2019 年 10 月 9 日
thanks for the help.

サインイン to comment.



Translated by