Imaging a 3d array with R - arrays

I have got a question regarding array's visualization in R. There is an image() function which makes a 2d-image based on the matrix that it is received; however, this function are not valid for arrays.
So, how is it possible to make a 3d-image through a 3d-array?
For instance:
y<-matrix(round(runif(144)),nrow = 12,ncol = 12,byrow = TRUE)
image(y, axes=F)
Now how we can make a 3d image if we have:
y=array(data=round(runif(12*12*12))+1,dim = c(12,12,12))
Regards,
Shapour

there is image3d function in misc3d:
library(misc3d)
library(rgl)
y=array(data=round(runif(12*12*12))+1,dim = c(12,12,12))
image3d(y)

Related

How to use IDL's min() function's min_subscript argument in for loop?

I have experience in python but am new to IDL. I am trying to write a function that will return two bins. I want to use the min function to get my bin edges. My issue is that I am trying to use the min_subscript argument to denote each bin edge, and I can't figure out how to do this in a for loop. I want to write my code so that each loop has 2 different min_subscript variables (the two edges of the bin), and these variables are written into their own arrays. Here is my code:
FUNCTION DBIN, radius, data, wbin, radbin, databin
FOR i = 0, N_ELEMENTS(radius)-1 DO BEGIN
l = lonarr(N_ELEMENTS(radius))
m = lonarr(N_ELEMENTS(radius))
junk1 = min(abs(radius - radius[i]), l[i])
junk2 = min(abs(radius - (radius[i] + wbin)), m[i])
radbin = lonarr(N_ELEMENTS(radius))
radbin[i] = radius[l[i]:m[i]]
databin = lonarr(N_ELEMENTS(data))
databin[i] = total(data[l[i]:m[i]])
ENDFOR
END
wbin is the desired bin width. The junk variables only exist for the purpose of getting the min_subscripts at those locations. The min_subscripts are the l[i]'s and the m[i]'s.
I appreciate any help!!
The min_subscript argument is trying to pass a value back to you, so you must pass a "named variable" to it. Named variables have pass by reference behavior. So you have to do it in two steps, something like:
junk1 = min(abs(radius - radius[i]), li)
l[i] = li
Above, li is a named variable, so it can receive and pass back the value. Then you can put it in your storage array.

Array of structures in MATLAB Simulink

I am trying to create an array of structures in Simulink and got some problems with it.
first of all i tried to create it directly in Simulink using this:
function a = fcn(Dibhole, t , x, const)
%#codegen
%Output = zeros(10,10);
f1 = 'number';
f2 = 'move';
cube = struct(f1, 0, f2, 0);
a = repmat(cube, 20, 10);
for i = 1:20
for j = 1:10
a(i,j).number = 0;
a(i,j).move = 0;
end
end
and i got this error:
Derived output was of type struct. 'Inherited type' is unsupported for this
type and a defined bus object must be used instead. Click on 'a' and
set data type for 'a' to be 'Bus: ', where '' is
the name of a bus object from the MATLAB workspace.
So i found some example how to create struct in Matlab and receive this to Simulink: http://blogs.mathworks.com/seth/2011/12/05/initializing-buses-using-a-matlab-structure/
That works perfectly but i still can't repeat this with array:
f1 = 'number';
f2 = 'move';
cube = struct(f1, 0, f2, 0);
myStruct2 = repmat(cube, 20, 10);
for i = 1:20
for j = 1:10
myStruct2(i,j).number = 1;
myStruct2(i,j).move = 1;
end
end
busInfo = Simulink.Bus.createObject(myStruct2);
Can anyone clarify to me what's the problem? Or maybe there is different way to create array of struct in Simulink?
Mihail
Simulink wants you to define the output of the function to be a bus.
As 'Bus: My_test_bus', for example.
Take a look at the Simulink Bus Editor. You can find it in any model under the menu, Edit->Bus Editor.
This would be a good start.
Rick, i think you are right!
i have tried this problem for a long time, and have got this results:
the irony is that I was never able to create array of structures BUT i did this with structure of arrays! :D
I made this steps for it:
to use structure of arrays we need to define and initialize it in some MATLAB function. Like this:
number = zeros(10,1);
move = zeros(10,1);
for i = 1:10
number(i,1) = i+1;
move(i,1) = i+2;
end
a = struct('numbers',number,'movement', move);
To work this data we must use Bus Selector.
So we have array in "numbers" and "movement".
BUT! Here we go, Rick: we must define type of output of MATLAB function like Bus! How to do this in simulink? i found this way:
in model properties in simulink Callbacks/PreLoadFcn define some function and in same folder as project create .m file named like this just defined function.
In this file create structure of array and define Bus type for it:
number = zeros(10,1);
move = zeros(10,1);
a = struct('numbers',number,'movement', move);
busInfo = Simulink.Bus.createObject(a);
Now we have Bus type for our structure at first loading of simulink model.
Last step: define MATLAB function output type directly.
in Model Explorer choose your MATLAB function. choose output variable. Set DataType for it: Bus:slBus1 (the name of this Bus type you can see in wokspace of matlab, because its a global variable).
That's all! now it works!
(tried to add pictures, but i have no enough reputation :( )
Now my program works in this way, but i also tried to create array of structures and still have the problems. i tried to create Bus for it, but can't transmit it to Bus Selector - it doesn't know what to do with structures... i also tried to add one more MATLAB function to create some data from structures and then display it, but it doesn't works too(

Trying to hide an object in an array when it is clicked, works without an array but not with? Actionscript 3.0

I am making a little game and I have an array. The array contains 4 characters. When one of the characters is clicked the opacity should turn to 0, if another is clicked the same should also happen.
So far I have put the array into a function but the function will only hide one of the characters, and not even the one which is clicked. Could anyone help me please? Here is the code I have:
for(var g:int = 0; g<ghostsL.length; g++){
ghostsL[g].addEventListener(MouseEvent.CLICK, clickGrey)
};
function clickGrey(e:MouseEvent):void{
this.ghostsL[i].alpha = 0;
var npoint:NPoint = new NPoint();
npoint.play();
};
We do not know what this.ghostsL[i] is.
Why don't you just do it this way:
function clickGrey(e:MouseEvent):void{
MovieClip(e.currentTarget).alpha = 0;
var npoint:NPoint = new NPoint();
npoint.play();
};

How to initialize an array of a List<Float>

I am learning to build a surface using JMonkey api. The class Surface has a method
createNurbsSurface(controlPoints, nurbKnots, uSegments, vSegments, basisUFunctionDegree, basisVFunctionDegree).
I am trying to make a simple example to understand the meaning of the arguments. However, I can't initialize the second argument:
List<Float>[] nurbKnots
I tried:
List<Float>[] nurbKnots = {new ArrayList<Float>()};
but it complains that you cannot create a generic array of List<Float>.
Could someone show me how to initialize this nurbKnots.
It works for non-generic List:
List[] listNonGeneric = new ArrayList[10];
But this won't work:
List<Float>[] listGeneric = new ArrayList<Float>[10];
You have to use:
List<List<Float>> nurbKnots = new ArrayList<>();
and pass the argument as
(List<Float>[])nurbKnots.toArray();
One friend helped. He told me:
In Java, it's not really possible to have arrays of generic types (safely). You have to allow unchecked assignment. Something like:
#SuppressWarnings("unchecked")
List<Float>[] f = new List[2];
f[0] = new ArrayList<Float>();
f[0].add(0.1);
f[1] = new ArrayList<Float>();
f[1].add(0.2);
But, it worked!

raw sound byteArray to float Array

I'm trying to convert the byteArray of a Sound Object to an array with floats. The Sound Object plays back fine & at full length, but the float Array i get from it is cut off (but sounds correct), so i must be doing something wrong in the conversion:
var s:Sound = mySound;
s.play(); // plays fine
var bytes:ByteArray = new ByteArray();
bytes.endian = Endian.LITTLE_ENDIAN;
s.extract(bytes, s.bytesTotal, 0);
var leftChannel:Array = new Array();
var rightChannel:Array = new Array();
bytes.position = 0;
while (bytes.bytesAvailable)
{
leftChannel.push(bytes.readFloat());
rightChannel.push(bytes.readFloat());
}
and this is what i get:
The top two channels are the original Sound Object.
The lower two is the float Array Data. I aligned them so you can see that the beginning is cut off and obviously the length is incorrect.
Thanks for any answers...
ok there were two problems:
the mp3 file i was importing was somehow corrupt, that caused the beginning to be cut off
the length i defined to extract was not correct, to find the full sound length use
var numTotalSamples:Number = int(s.length * 44.1); //assuming 44.1kHz sample rate
then:
s.extract(bytes, numTotalSamples, 0);

Resources