Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Here is my matlab code:
gg=imread('fsu_Westcott.jpg');
data1=gg(:,1);
histArray1=zeros(1,256);
x=0:1:255;
for n=1:length(data1)
histArray1(1,data1(n)+1)=histArray1(1,data1(n)+1)+1;
end
for n=1:length(data1)
number1=sum(histArray1(1:n));
end
plot(x,number1,'r')
Why it shows "Index exceeds matrix dimensions."? before I finally plot? I am new and thanks in advance! :)
On the line:
number1=sum(histArray1(1:n));
you are asking for cells 1 to n of array histArray1 but n goes from 1 to length(data1) which is larger than the length of histArray1 (256). So it is out of bounds.
This loop:
for n=1:length(data1)
number1=sum(histArray1(1:n));
end
seems unnecessary if you want to plot the histogram anyway.
One more tip, there is a function called hist which you could use to both compute the histogram and plot the result in a barchart in one line:
hist(data1(:), 0:255)
To me it looks like you have a couple things you'll want to fix. To fix the error on the line Simon pointed out, I think you'll want to have your 2nd For loop going from 1 to length(histArray) (or 256) instead of length(data1). The second problem I see is that in the 2nd For loop you aren't building an array--you're just redefining the variable number1 over and over. You should probably put number1(n)=sum(histArray1(1:n)); inside that for loop instead. This doesn't have to do with the error you're seeing but it might help you get what you want out of the script.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have an array of structs. Every struct holds a set of data for one single measure. Matlab gives me an error when I try to plot this data.
Expected one output from a curly brace or dot indexing expression, but there were 361 results.
How should I rewrite my plot code?
plot(result.structArray_A(:).nonArrayValue_X, result.structArray_A(:).nonArrayValue_Y);
I have found a solution. Simply writing the following does work, even if the syntax does look odd:
plot([result.structArray_A.nonArrayValue_X], [result.structArray_A.nonArrayValue_Y]);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 6 years ago.
Improve this question
I'm really new to MATLAB and have to implement some functions. To start, I'm supposed to make a function about a mean square error, for which I have the following formula:
My goal is to implement this in MATLAB. I've tried to analyse this for days, but I'm still lost. I guess the first thing to do is to create a function and then put all of the calculations. Should I declare variables like N though? Or can I just use them in the calculations without declaring? I'm just looking for some advises that would help me get started, I have no one else to ask about this, so a short guide/tips on how to take down this particular example would be amazing. Thank you in advance!
For a mean square error you need two inputs, Y and Y_bar and one output E. You don't need to declare N because it is implied by the length of each of your inputs. I'm going to assume that your inputs are both column vectors and are the same length.
function E = MSE(Y, Y_bar)
N = size(Y,1);
E = sum((Y-Y_bar).^2)/N
end
You should save this code a an .m file named mse.m and make sure it is in your working directory. If you don't know what that means, you need to look it up.
This is extremely basic MATLAB though, before you continue with anything, I think you should start by working through some beginners guides. As mentioned, stackoverflow is not the place to learn the very basics of a programming language.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
i keep getting the error "Error using -""Matrix dimensions must agree"
for this piece of code. Can anybody help me and identify where i'm going wrong? I should be getting 8 plots of f vs d.
P=500;
N=1:1:8;
a=-0.4;
b=-1;
Kd=700;
d50star=(1./(10.^(Kd*(N.^a)*(P-115).^b)))
w0=0.45;
d=0:0.1:10
d50N0=5;
if d50star < 0.33;
w=(1-(2.3*d50star))*w0
else
w=(3.4-(5.5*d50star))*w0
end
d50=(1-d50star)*d50N0;
f=1-(1./((1+exp((d-d50)/w))))
There are at least two errors in the script:
1) in the last line [f=1-(1./((1+exp((d-d50)/w))))]
d size is 1x101
d50 size is 1x8
The size inconsistency is related to the definition of:
N=1:1:8; and
d=0:0.1:10
2) depending on the algoroth, it could be ./w instead of /w
Hope this helps.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm working on a matlab program where at one point I create a 3x1001 matrix called 'bots'. I checked that Matlab created this matrix correctly, but as soon as Matlab enters a while-loop, there is an extra matrix called 'bots' of dimensions 1x1001 with different doubles in it.
...
bots=zeros([3 1001]);
bots(1,:)=botsStartPlace;
bots(2,1:nbBots-1)=botsStartPlace(2:nbBots);
bots(2,nbBots)=length(indexTable);
whos bots % bots 3x1001 24024 double
while(going)
whos bots %bots 3x1001 24024 double
%bots 1x1001 8008 double
....
Anyone has a clue why matlab is doing this? It's freaking me out! :p
The following snippet does not create a "ghostly" instance:
bots = zeros(3, 1001);
whos
going = 2;
while (going)
whos
going = going - 1;
end
The output is:
bots 3x1001 24024 double
going 1x1 8 double
So the problem cannot really come from the while-loop itself.
I have found the problem! My bad, there never was a ghost! Matlab had already gone through the loop once and was ordering the 'whos' output of different calls to the function together which gave me the impression that there where multiple instances. Instead I was just accidentally hurting the matrix in the loop and thereby changing it's dimensions! Sorry for your time! :(
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a mathematical data where it would be very convenient to have the index to start from zero like
a=sparse([],[],[],30,1);
>> a(0)=someValueHere
Subscript indices must either be real positive integers or logicals.
but Matlab by default offers only the index to start from 1. Is there some easy hack or trick by which I could still assign a(0) so that I don't need to create a dummyVar a0 for the value or append the value at the end?
So how to get assignment such as a(0) in Matlab? Every time zero-index called catch the error and return someValueHere instead of the warning?
To get MATLAB's index to start from 0 you'll need to make an large set of object classes that emulate regular numeric classes, but behave differently with functions such as subsassgn(), subsref() etc.
Maybe someone was crazy enough to do it somewhere, I'd expect this to take weeks to months of work to actually work properly.
There is a discussion on the matlab index issue: http://www.mathworks.cn/matlabcentral/newsreader/view_thread/285566
Maybe you can write a function like
function t=C_index(x)
t = x + 1;
Then you can write something like y(C_index(0)) to get the first value in vector y.
In Addition,
t=#(x) x+1
y(t(0))
should work.