plotting separate parts of a column on different figures

조회 수: 1 (최근 30일)
Benjamin
Benjamin 2019년 3월 27일
편집: Stephen23 2019년 3월 28일
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.
  댓글 수: 6
Stephen23
Stephen23 2019년 3월 27일
편집: Stephen23 2019년 3월 27일
"But all of them are different sizes though, they can't easily go into an array"
That is exactly what cell arrays are for.
"The alternative is I just import the sheet name "Master" and then I have all the data."
That would likely be the best solution.
"Then I have the problem that I initially asked in this question. By loading them in separately, I don't have to separate them in MATLAB."
Processing the data within MATLAB is much more efficient than importing data from file multiple times (hard-drives are very slow!). Simply import that data all at once, use diff to detect where eta changes, then loop over those indices and plot the data.
Benjamin
Benjamin 2019년 3월 27일
I am not sure how to use diff to detect eta changes and plot appropriately. Any chance you could show me what you mean?

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2019년 3월 27일
편집: Stephen23 2019년 3월 27일
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)))
end
  댓글 수: 8
Benjamin
Benjamin 2019년 3월 28일
So I took your advice, removed the loop and vectorized it with r.^. Seems to work well. One issue that I get when I replace r with R is that the vectors must be the same length. The number of R values changes on each eta value. Is there a way to circumvent this?
Stephen23
Stephen23 2019년 3월 28일
편집: Stephen23 2019년 3월 28일
"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
>> R.^eta
ans =
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by