problem with nested loop

3 ビュー (過去 30 日間)
Matlabuser
Matlabuser 2015 年 4 月 30 日
編集済み: Stephen23 2015 年 5 月 4 日
Hi, I am trying to write a code with nested for loop and if statement. I want to create a matrix in such way that, for one value of y, I have different values of x and z, and again for another value of y, I have same values for x but different values of z.
load a.dat;
x=a(:,1);
y=a(:,2); z=a(:,3); for j=min(y):1:max(y)
for i=1:10
if j=10
A(:,i)=z(i)
else if
j=j+1
end
end
end
I am not able to apply if statement for y.
Could anyone please help me?
  4 件のコメント
Matlabuser
Matlabuser 2015 年 4 月 30 日
Actually, I have to rearrange my data in a proper format. I have 24 X 3 matrix, with x,y,z values.z values are calculated on x and y values. For one value of y, I have different values of x and z, for instance, x=[0,1,2,3,4,5];y=[1,1,1,1,1,1];z=[0.1,0.4,0.3,0.5,0.9,1.1] then for other value of y, like y=2, I have same values of x but different values of z.
Now I want to rearrange the data in 6 X 4 dimension such that for y=1 and all values of x, I can put values of z in 1st column of an empty matrix. Then for second value of y, z in second column.
Hope this time I am clear.
Stephen23
Stephen23 2015 年 4 月 30 日
If you upload your actual data matrix then we can try this too! Please upload the data (in a textfile or .mat file) using the paperclip button that you will find above the textbox, and note that you will need to push both buttons: Choose file and Attach file.

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2015 年 4 月 30 日
編集済み: Stephen23 2015 年 5 月 4 日
You have x and y values, and it seems that you want to use these as indices for allocating data values into another matrix. This can be achieved very easily using sub2ind. Here is a simple demo-matrix where the first column contains row indices, the second is column contains column indices, and the third contains the data:
>> A = [2,1,5;1,3,7;2,3,0;1,1,9;2,2,4]
A =
2 1 5
1 3 7
2 3 0
1 1 9
2 2 4
% row col data
then we extract the row and column indices:
>> R = A(:,1);
>> C = A(:,2);
create the output matrix and use sub2ind to get the linear indices for those row/column indices:
>> Z = nan(max(R),max(C));
>> X = sub2ind(size(Z), R, C);
and finally use the linear indices to put the data values into their right locations:
>> Z(X) = A(:,3)
Z =
9 NaN 7
5 4 0
Note that MATLAB has one-based indexing, so you will need to add one to the values that you have in your example data.
  1 件のコメント
Matlabuser
Matlabuser 2015 年 5 月 4 日
Thank you very much !!!!

サインインしてコメントする。

その他の回答 (1 件)

Søren Jensen
Søren Jensen 2015 年 4 月 30 日
A(:,i)= z(i)
do you mean A(:,i)= z(:,i) or A(:,i)= z(i,:)?
When is A defined?
i see 2 for statements and 1 if statement, and only 2 "end", so some of your code seems to be missing.. what is the purpose of the code?
  1 件のコメント
Matlabuser
Matlabuser 2015 年 4 月 30 日
Sory, I am a bit bad in Matlab. Now probably,my question is clear

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by