need general coding/ command to separate matrix data

I have a for loop y= 0:2:8 and whenever the for loop value chnaged it generates two values which was stored in a matrix. Now I have a complete matrix in which column one have y=0 then several values in a rows then y changed and several vlaues in a row as shown in fig.
final matrix is:
0 5 9
0 7 3.3
0 5.5 9
1 4.0 3.5
1 2 3.3
...................... so on.
Question: I need a general coding that store number of rows in a seperate matrix/ vector when y value is fixed. I mean when y=0 then all rows with y=0 stored in a seperate vector/ matrix. Then again y changed and results in several rows. These rows stored in a seperate vector/matrix.
Your support will be highly appreciated.
Regards

4 Comments

ix=(y==0); % the logical addressing vector -- NOTA BENE: size(y,1) must equal size(M,1)
M(ix,:)=RHS; % store vector RHS at those rows -- NOTA BENE: numel(RHS) must equal size(M,2)
I try it but it will not work.
I broadly explain my situation. I have a matrix in which first column is the value of y. In my coding y=0:2:8. It means start from zero to eight with increment of 2 in every loop.
Situation is I run code, for loop exceute and we get a matrix. now i have a matrix and need to build a new code. The value of first column remian fixed upto several rows then changed and so on. I need all rows in a seperate matrix with a single value of y. If y=0 then all rows with y=0. If y=2 then all rows with y=2 and so on.
Please reconsider this explanation.
Keeping data together is usually better than splitting it apart. How will you process the data?
Now I send you my coding but formula is shorter my formula to simpler. So, when v exceute it give us matrix of three column. Important is value of first column is fixed upto seperate rows. I need all those values in a seperate matrix. reason is that for a fixed value of beta there are certian values in column 2 and 3. I need to draw graph using these values. Then again beta change and i have certian set of values and i need again draw graph using "hold on" command.
If you undertsnad my question and you think another way to tackle this then please add your coding in it.
thanks in advance
clear all
c1= 0.4;
v=[];
betavalues= 0:2:4;
a= zeros(length(betavalues),1);
for i = 1:length(betavalues)
a(i)= betavalues(i);
beta=a(i)
lamdavalues= 1:1:10;
b= zeros(length(lamdavalues),1);
for j=1:length(lamdavalues)
b(j)= lamdavalues(j);
lamda=b(j);
x1= beta*lamdavalues*c1;
v(end+1,:)=[beta lamda x1];
end
end

Sign in to comment.

 Accepted Answer

Try this:
% Fake data
finalmatrix=sortrows(randi(10,30,3),1)
finalmatrix = 30×3
1 7 1 2 6 10 2 7 9 3 10 9 3 7 1 3 3 5 3 2 4 4 4 5 4 8 8 4 6 2
y = finalmatrix(:,1);
n=diff(find([true; diff(y)~=0; true]))
n = 10×1
1 2 4 3 1 5 3 5 4 2
splitmatrix=mat2cell(finalmatrix,n,size(finalmatrix,2));
splitmatrix{:}
ans = 1×3
1 7 1
ans = 2×3
2 6 10 2 7 9
ans = 4×3
3 10 9 3 7 1 3 3 5 3 2 4
ans = 3×3
4 4 5 4 8 8 4 6 2
ans = 1×3
5 2 1
ans = 5×3
6 2 2 6 10 1 6 1 6 6 6 2 6 5 5
ans = 3×3
7 3 9 7 3 7 7 4 8
ans = 5×3
8 9 7 8 8 2 8 3 3 8 2 5 8 9 8
ans = 4×3
9 6 6 9 9 7 9 4 7 9 1 6
ans = 2×3
10 8 7 10 6 3

4 Comments

Thanks bruno luong. It's working. I have just one question on it. when matrix splits then any split matrix stored in a new matrix? Like shown below two different matrixes one with column 1 value of 9 (constant) other is with constant value of 10.
I need to plot a graph using cloumn 2 on x-axis and column 3 on y-axis (when column 1 have a constant value) then i applied command "hold on" and draw graph of other columns and so on.
ans = 4×3
9 6 6
9 9 7
9 4 7
9 1 6
ans = 2×3
10 8 7
10 6 3
The matrices are stored in a cell vertical vector (cell array of #columns = 1)
Thanks once again.
If you add a command for my reference about above mentioned querry than i shall be very thankful to you. I am puzzling and my issue for plotting not resolved.
I got totally lost in the explanation above, but if @Bruno Luong's solution for splitmatrix is the right data, then
figure
hold on
cellfun(@(c)plot(c(:,2),c(:,3)),splitmatrix)
should be close to what you're looking for.
As always, "salt to suit"...

Sign in to comment.

More Answers (0)

Categories

Asked:

on 6 Sep 2022

Commented:

dpb
on 7 Sep 2022

Community Treasure Hunt

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

Start Hunting!