plotting separate parts of a column on different figures
2 views (last 30 days)
Show older comments
I have a large matrix. In the 3rd column, I have my "eta" values. In my 4th column, I have "r" values. In my 8th column, I have "g" values.
Eta is the same as you go down column for a while. How can I plot g vs. r as long as eta is the same, then make a new plot with g vs r anytime eta changes?
Maybe the matrix needs to be reshaped first? Note that the number of eta values that are the same can change.
Stephen23 on 27 Mar 2019
Edited: Stephen23 on 27 Mar 2019
Following your description "I have a large matrix. In the 3rd column, I have my "eta" values. In my 4th column, I have "r" values. In my 8th column, I have "g" values", here is one simple loop:
% Random fake data with contiguous eta groups:
mat = rand(32,8);
mat(:,3) = sort(randi(9,32,1));
% Detect eta groups (i.e. changes in eta):
bnd = find([true;diff(mat(:,3));true]);
% Plot data for each eta group:
for k = 1:numel(bnd)-1
figure() % using ONE figure is usually simpler.
idx = bnd(k):bnd(k+1)-1;
G = mat(idx,8);
R = mat(idx,4);
plot(R,G) % or plot(G,R), whatever you want
title(sprintf('eta = %d',mat(bnd(k),3)))
Stephen23 on 28 Mar 2019
Edited: Stephen23 on 28 Mar 2019
"The number of R values changes on each eta value."
It is not clear to me what this means.
"Is there a way to circumvent this?"
As far a I can tell you could use the dimensions of your arrays. For example, if R is a column vector and eta a row vector, then they can be different lengths and you wll get all values at the output:
>> eta = [1,2,3]; % row
>> R = [4;5;6;7]; % column
4 16 64
5 25 125
6 36 216
7 49 343
Use higher dimensions if required: e.g. if R is an MxN matrix that you do not want to reshape, then reshape eta to a 1x1xP vector, it works in exactly the same way.
Using the dimensions ot arrays is one of MATLAB's most powerful features.
For MATLAB versions prior to R2016b you will need to use bsxfun.
More Answers (0)
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!